IOS基础:常用的小方法

2020-10-20  本文已影响0人  时光啊混蛋_97boy

原创:知识点总结性文章
创作不易,请珍惜,之后会持续更新,不断完善
个人比较喜欢做笔记和写总结,毕竟好记性不如烂笔头哈哈,这些文章记录了我的IOS成长历程,希望能与大家一起进步
温馨提示:由于简书不支持目录跳转,大家可通过command + F 输入目录标题后迅速寻找到你所需要的内容

目录

一、数字

获取随机数

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    int randomNumber = [self getRandomNumber:0 to:100];
    NSLog(@"随机数:%d",randomNumber);
}

// 获取一个随机整数,范围在[from,to],包括from,包括to
- (int)getRandomNumber:(int)from to:(int)to
{
    return (int)(from + (arc4random() % (to - from + 1)));
}

输出结果为:

2020-09-25 11:00:54.338468+0800 FunctionCodeBlockDemo[5532:18165243] 随机数:65

二、文本

字体

打印系统中所有字体的类型名字
- (void)getFontNames
{
    NSArray *familyNames = [UIFont familyNames];
    for(NSString *familyName in familyNames )
    {
        printf("字体家族名称: %s \n", [familyName UTF8String]);
        
        NSArray *fontNames = [UIFont fontNamesForFamilyName:familyName];
        for(NSString *fontName in fontNames)
        {
            printf("\t字体名称: %s \n", [fontName UTF8String]);
        }
    }
}

输出结果为:

字体家族名称: Academy Engraved LET 
    字体名称: AcademyEngravedLetPlain 
字体家族名称: Al Nile 
    字体名称: AlNile 
    字体名称: AlNile-Bold 
.......

点击链接跳转

- (void)ClickLinkToJump
{
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"百度"];
    
    NSDictionary *linkDict = @{ NSLinkAttributeName:[NSURL URLWithString:@"http://www.baidu.com"] };
    
    [str setAttributes:linkDict range:[[str string] rangeOfString:@"百度"]];
    
    self.textView.attributedText = str;
    self.textView.editable = NO;
    self.textView.dataDetectorTypes = UIDataDetectorTypeLink;
}

运行效果如下:

点击链接跳转

点击背景隐藏键盘

// 点击背景隐藏键盘
- (void)tapBackgroundHideKeyboard
{
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)];
    
    // 设置成NO表示当前控件响应后会传播到其他控件上,默认为YES
    tapGestureRecognizer.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tapGestureRecognizer];
}

- (void)keyboardHide:(UITapGestureRecognizer*)tap
{
    [self.textField resignFirstResponder];
    NSLog(@"点击了背景,键盘被隐藏了");
}

输出结果为:

2020-09-25 11:23:13.630446+0800 FunctionCodeBlockDemo[5946:18186936] 点击了背景,键盘被隐藏了

三、导航栏

获取当前页面的控制器

- (UIViewController *)currentViewController
{
    UIViewController* currentViewController = [[[UIApplication sharedApplication] delegate] window].rootViewController;
    BOOL runLoopFind = YES;
    while (runLoopFind)
    {
        if (currentViewController.presentedViewController)
        {
            currentViewController = currentViewController.presentedViewController;
            
        }
        else if ([currentViewController isKindOfClass:[UINavigationController class]])
        {
            UINavigationController* navigationController = (UINavigationController* )currentViewController;
            currentViewController = [navigationController.childViewControllers lastObject];
            
        }
        else if ([currentViewController isKindOfClass:[UITabBarController class]])
        {
            UITabBarController* tabBarController = (UITabBarController* )currentViewController;
            currentViewController = tabBarController.selectedViewController;
            
        }
        else
        {
            NSUInteger childViewControllerCount = currentViewController.childViewControllers.count;
            
            if (childViewControllerCount > 0)
            {
                currentViewController = currentViewController.childViewControllers.lastObject;
                return currentViewController;
            }
            else
            {
                return currentViewController;
            }
        }
    }
    
    return currentViewController;
}

在导航栏栈中寻找到指定的页面返回

// 在导航栏栈中寻找到指定的页面返回
- (void)backToNeedViewController
{
    NSMutableArray *viewControllers = [[NSMutableArray alloc] init];
    
    for (UIViewController *viewController in [self.navigationController viewControllers])
    {
        [viewControllers addObject:viewController];
    
        if ([viewController isKindOfClass:[NeedViewController class]])
        {
            break;
        }
    }
    
    [self.navigationController setViewControllers:viewControllers   animated:YES];
}

设置导航栏全透明

- (void)viewWillAppear:(BOOL)animated
{
    // 设置导航栏背景图片为一个空的image,这样就透明了
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    
    // 去掉透明后导航栏下边的黑边
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
}

- (void)viewWillDisappear:(BOOL)animated
{
    // 如果不想让其他页面的导航栏变为透明 需要重置
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    
    [self.navigationController.navigationBar setShadowImage:nil];
}

禁止侧滑返回上个页面功能

- (void)disableSideslipReturn
{
    // 1.首先把顶部左侧返回按钮隐藏掉
    self.navigationItem.hidesBackButton = YES;
    
    // 2.再禁止页面左侧滑动返回
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)])
    {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}

如果仅仅需要禁止此单个页面返回,还需要重新开放侧滑权限。

- (void)viewWillDisappear:(BOOL)animated
{
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)])
    {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
}

更改导航栏的返回按钮的标题与颜色

- (void)changeNavigationBarTitleAndColor
{
    // 返回按钮
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
    
    // 修改返回按钮的颜色
    self.navigationController.navigationBar.tintColor = [UIColor redColor];

    // 设置导航条的色调
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    
    // 导航栏默认是半透明状态
    self.navigationController.navigationBar.backgroundColor = [UIColor blueColor];
    
    // 导航栏标题颜色
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    
    self.navigationController.navigationBar.translucent = NO;
}

运行效果如下:

更改导航栏的返回按钮的标题与颜色

Demo

Demo在我的Github上,欢迎下载。
FunctionCodeBlock

参考文献

上一篇下一篇

猜你喜欢

热点阅读