iOS与导航栏有关的神技

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_25078673/article/details/52842259

  1、导航栏上的返回按钮

<span style="font-size:24px;">   </span><span style="font-size:18px;">self.navigationController.navigationBar.topItem.title=self.message;
   self.navigationController.navigationBar.tintColor=[UIColor blackColor];
   UIBarButtonItem*backButton = [[UIBarButtonItemalloc] initWithTitle:@"返回"style:UIBarButtonItemStyleBorderedtarget:selfaction:@selector(PopViewController)];
   self.navigationItem.leftBarButtonItem= backButton;</span>

   2、导航栏上的按钮自定义

    UIButton *editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    editBtn.frame = CGRectMake(0, 0, 60, 40);
    [editBtn setImage:[UIImage imageNamed:@"nav_right_edit"] forState:UIControlStateNormal];
    [editBtn setTitle:@"完成" forState:UIControlStateSelected];
    [editBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [editBtn addTarget:self action:@selector(editButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    //创建navigationBar右侧Item
    self.editButton = [[UIBarButtonItem alloc] initWithCustomView:editBtn];

  3、导航栏的一些设置

    UINavigationBar *navigationBarAppearance = [UINavigationBar appearance];
    navigationBarAppearance.barTintColor = [UIColor whiteColor];
    navigationBarAppearance.tintColor = [UIColor colorWithRed:0.098 green:0.098 blue:0.098 alpha:0.7];
    navigationBarAppearance.translucent = NO;
    //设置导航栏上标题的字体大小及颜色
    [navigationBarAppearance setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:20],
       NSForegroundColorAttributeName:[UIColor blackColor]}];
    //设置导航栏的背景图片
//  [navigationBarAppearance setBackgroundImage:[UIImage imageNamed:@"header_background"] forBarMetrics:UIBarMetricsDefault];
    //设置导航栏的背景颜色
    [navigationBarAppearance setBackgroundColor:[UIColor whiteColor]];
    //自定义返回按钮的图片样式
//  UIImage *backButtonImage = [[UIImage imageNamed:@"nav_back"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
//  [[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    //设置返回按钮标题的偏移 
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];

    4、透明设置和清除黑线

   //导航栏变为透明
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:0];
   //让黑线消失的方法
    self.navigationController.navigationBar.shadowImage=[UIImage new];

   以上内容为自己在开发总结,希望可以帮助到有需要的人。如有遗漏或者不足之处,还请各位多加指教。也可以加入 QQ群:181798048 相互学习交流,iOS沪上代码小子书


猜你喜欢

转载自blog.csdn.net/qq_25078673/article/details/52842259