一:首先查看一下关于UINavigationController的定义

  1. NS_CLASS_AVAILABLE_IOS(2_0) @interface UINavigationController : UIViewController
  2.  
  3. //UINavigationController初始化,自定义NavigationBar,自定义toolbar
  4. - (instancetype)initWithNavigationBarClass:(nullable Class)navigationBarClass toolbarClass:(nullable Class)toolbarClass NS_AVAILABLE_IOS(5_0);
  5.  
  6. //UINavigationController初始化,导航控制器的根控制器
  7. - (instancetype)initWithRootViewController:(UIViewController *)rootViewController;
  8.  
  9. //压栈:将目标控制器压入栈中
  10. - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;
  11.  
  12. //弹栈:将栈顶控制器从栈中弹出
  13. - (nullable UIViewController *)popViewControllerAnimated:(BOOL)animated;
  14.  
  15. //弹栈:弹到指定的目标控制器
  16. - (nullable NSArray<__kindof UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated;
  17.  
  18. //弹栈:弹到根控制器
  19. - (nullable NSArray<__kindof UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated;
  20. //导航栈的栈顶视图 只读 就是某个导航栈的栈顶视图,和导航息息相关
  21. @property(nullable, nonatomic,readonly,strong) UIViewController *topViewController;
  22. //当前显示的控制器 只读 visibleViewController和哪个导航栈没有关系,只是当前显示的控制器,也就是说任意一个导航的visibleViewController所返回的值应该是一样的
  23. @property(nullable, nonatomic,readonly,strong) UIViewController *visibleViewController;
  24.  
  25. //栈里的视图控制器数组
  26. @property(nonatomic,copy) NSArray<__kindof UIViewController *> *viewControllers;
  27.  
  28. //替换栈中的视图控制器数组
  29. - (void)setViewControllers:(NSArray<UIViewController *> *)viewControllers animated:(BOOL)animated NS_AVAILABLE_IOS(3_0);
  30.  
  31. //是否隐藏导航栏
  32. @property(nonatomic,getter=isNavigationBarHidden) BOOL navigationBarHidden;
  33.  
  34. //设置导航栏隐藏 是否有动画
  35. - (void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated;
  36.  
  37. //导航栏
  38. @property(nonatomic,readonly) UINavigationBar *navigationBar;
  39.  
  40. //toolbar是否隐藏
  41. @property(nonatomic,getter=isToolbarHidden) BOOL toolbarHidden NS_AVAILABLE_IOS(3_0);
  42.  
  43. //toolbar是否隐藏 是否有动画
  44. - (void)setToolbarHidden:(BOOL)hidden animated:(BOOL)animated NS_AVAILABLE_IOS(3_0);
  45.  
  46. //toolbar对象
  47. @property(null_resettable,nonatomic,readonly) UIToolbar *toolbar NS_AVAILABLE_IOS(3_0);
  48.  
  49. //委托
  50. @property(nullable, nonatomic, weak) id<UINavigationControllerDelegate> delegate;
  51.  
  52. //边缘侧滑返回手势
  53. @property(nullable, nonatomic, readonly) UIGestureRecognizer *interactivePopGestureRecognizer NS_AVAILABLE_IOS(7_0);
  54.  
  55. //展示视图控制器
  56. - (void)showViewController:(UIViewController *)vc sender:(nullable id)sender NS_AVAILABLE_IOS(8_0); // Interpreted as pushViewController:animated:
  57.  
  58. //输入键盘出现时将导航栏隐藏 IOS8特性
  59. @property (nonatomic, readwrite, assign) BOOL hidesBarsWhenKeyboardAppears NS_AVAILABLE_IOS(8_0);
  60.  
  61. //滚动页面时隐藏Bar IOS8特性
  62. @property (nonatomic, readwrite, assign) BOOL hidesBarsOnSwipe NS_AVAILABLE_IOS(8_0);
  63.  
  64. //获取能够隐藏navigationBar的滑动手势 只读
  65. @property (nonatomic, readonly, strong) UIPanGestureRecognizer *barHideOnSwipeGestureRecognizer NS_AVAILABLE_IOS(8_0);
  66.  
  67. //当设置为true时,横向方向时隐藏NavigationBar
  68. @property (nonatomic, readwrite, assign) BOOL hidesBarsWhenVerticallyCompact NS_AVAILABLE_IOS(8_0);
  69.  
  70. //当设置为true时,如果有没处理的点击手势就会隐藏和现实navigationBar
  71. @property (nonatomic, readwrite, assign) BOOL hidesBarsOnTap NS_AVAILABLE_IOS(8_0);
  72.  
  73. //获取能够隐藏navigationBar的点击手势 只读
  74. @property (nonatomic, readonly, assign) UITapGestureRecognizer *barHideOnTapGestureRecognizer NS_AVAILABLE_IOS(8_0);
  75.  
  76. @end

知识点1:UINavigationController  UINavigationBar  UINavigationItem三者的关系

通俗地说就是,UINavigationController是个容器,里面可以装很多UIViewController。装这么多UIViewController让用户怎么控制它们呢,总得有个工具吧。这个工具就是UINavigationBar。一个容器就这么一个bar,相当于控制台吧。但是,管理那么多UIViewController,控制台上得按钮啊、标题啊,都千篇一律是不是看起来太无聊了。为了解决这个问题,UINavigationController为每个UIViewController生成一个UINavigationItem,通过这个UINavigationItem可以改变控制台“上面”得按钮和标题。如果你不自定义UINavigationItem,UINavigationController会使用默认的;

UINavigationController是UIViewController的子类,UINavigationBar是UIView的子类。

UINavigationBar是UINavigationController的一个组成部分,就是上面的那个导航栏。

UINavigationBar又有UINavigationItem组成。

UINavigationItem则有title,按钮,提示文本等组成,就是我们看到的title文字,右上角的按钮。

navigation item在navigation Bar代表一个viewController,具体一点儿来说就是每一个加到navigationController的viewController都会有一个对应的navigationItem一个导航控制器控制多个视图,NavigationBar上的leftItem,rightItem,title是由当前的视图控制器控制的

二:UINavigationControllerDelegate的定义内容

  1. /**
  2. * 将要显示目标控制器
  3. *
  4. * @param navigationController 当前导航控制器
  5. * @param viewController 目标视图控制器
  6. * @param animated 动画
  7. */
  8. - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
  9. /**
  10. * 目标控制器最终显示
  11. *
  12. * @param navigationController 当前导航控制器
  13. * @param viewController 目标视图控制器
  14. * @param animated 动画
  15. */
  16. - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
  17. /**
  18. * 横竖屏切换
  19. *
  20. * @param navigationController 当前导航控制器
  21. *
  22. * @return 横竖屏方向
  23. */
  24. - (UIInterfaceOrientationMask)navigationControllerSupportedInterfaceOrientations:(UINavigationController *)navigationController NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
  25. /**
  26. * 横竖屏切换
  27. *
  28. * @param navigationController 当前导航控制器
  29. *
  30. * @return 横竖屏方向
  31. */
  32. - (UIInterfaceOrientation)navigationControllerPreferredInterfaceOrientationForPresentation:(UINavigationController *)navigationController NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
  33. /**
  34. * 转场动画
  35. *
  36. * @param navigationController 当前导航控制器
  37. * @param animationController 动画控制器
  38. *
  39. * @return 转场动画
  40. */
  41. - (nullable id <UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController
  42. interactionControllerForAnimationController:(id <UIViewControllerAnimatedTransitioning>) animationController NS_AVAILABLE_IOS(7_0);
  43. /**
  44. * 转场动画
  45. *
  46. * @param navigationController 当前导航控制器
  47. * @param operation 动画类型枚举
  48. * @param fromVC 起始视图控制器
  49. * @param toVC 目标视图控制器
  50. *
  51. * @return 转场动画
  52. */
  53. - (nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
  54. animationControllerForOperation:(UINavigationControllerOperation)operation
  55. fromViewController:(UIViewController *)fromVC
  56. toViewController:(UIViewController *)toVC NS_AVAILABLE_IOS(7_0);

UINavigationController的delegate方法我平时的开发工作中很少用到

三:UIViewController (UINavigationControllerItem)分类

  1. @interface UIViewController (UINavigationControllerItem)
  2. //导航栏上面用户自定义视图
  3. @property(nonatomic,readonly,strong) UINavigationItem *navigationItem;
  4. //推送时隐藏bottom
  5. @property(nonatomic) BOOL hidesBottomBarWhenPushed;
  6. //下级视图的导航控制器
  7. @property(nullable, nonatomic,readonly,strong) UINavigationController *navigationController;
  8.  
  9. @end

知识点1:UINavigationControllerItem因为扩展在UIViewController上,所以可以直接在UIViewController中使用下面的代码进行调用跟设置;

self.navigationItem.title = @"UINavigationBar使用总结";

四:UIViewController (UINavigationControllerContextualToolbarItems)分类 拓展toolbarItems属性

  1. @interface UIViewController (UINavigationControllerContextualToolbarItems)
  2. //属性设置工具条中包含的按钮
  3. @property (nullable, nonatomic, strong) NSArray<__kindof UIBarButtonItem *> *toolbarItems NS_AVAILABLE_IOS(3_0);
  4. - (void)setToolbarItems:(nullable NSArray<UIBarButtonItem *> *)toolbarItems animated:(BOOL)animated NS_AVAILABLE_IOS(3_0);
  5.  
  6. @end

五:关于UINavigationBar定义内容

  1. NS_CLASS_AVAILABLE_IOS(2_0) @interface UINavigationBar : UIView <NSCoding, UIBarPositioning>
  2.  
  3. @property(nonatomic,assign) UIBarStyle barStyle;
  4.  
  5. @property(nullable,nonatomic,weak) id<UINavigationBarDelegate> delegate;
  6.  
  7. //Translucent设置成透明度,设置成YES会有一种模糊效果
  8. @property(nonatomic,assign,getter=isTranslucent) BOOL translucent NS_AVAILABLE_IOS(3_0) UI_APPEARANCE_SELECTOR;
  9.  
  10. //UINavigationBar上面不只是简单的显示标题,它也将标题进行了堆栈的管理,每一个标题抽象为的对象在iOS系统中是UINavigationItem对象,我们可以通过push与pop操作管理item组。
  11. //向栈中添加一个item,上一个item会被推向导航栏的左侧,变为pop按钮,会有一个动画效果
  12. - (void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated;
  13. //pop一个item
  14. - (nullable UINavigationItem *)popNavigationItemAnimated:(BOOL)animated;
  15. //当前push到最上层的item
  16. @property(nullable, nonatomic,readonly,strong) UINavigationItem *topItem;
  17. //仅次于最上层的item,一般式被推向导航栏左侧的item
  18. @property(nullable, nonatomic,readonly,strong) UINavigationItem *backItem;
  19. //获取堆栈中所有item的数组
  20. @property(nullable,nonatomic,copy) NSArray<UINavigationItem *> *items;
  21. //设置一组item
  22. - (void)setItems:(nullable NSArray<UINavigationItem *> *)items animated:(BOOL)animated;
  23.  
  24. //系统类型按钮文字颜色
  25. @property(null_resettable, nonatomic,strong) UIColor *tintColor;
  26.  
  27. //通过barTintColor来设置背景色
  28. @property(nullable, nonatomic,strong) UIColor *barTintColor NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;
  29.  
  30. //设置工具栏背景和阴影图案
  31. - (void)setBackgroundImage:(nullable UIImage *)backgroundImage forBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;
  32. - (nullable UIImage *)backgroundImageForBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;
  33.  
  34. //通过背景图片来设置导航栏的外观
  35. - (void)setBackgroundImage:(nullable UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
  36.  
  37. - (nullable UIImage *)backgroundImageForBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
  38.  
  39. //背景阴影图片 - 即分割线
  40. @property(nullable, nonatomic,strong) UIImage *shadowImage NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;
  41.  
  42. //标题的富文本
  43. @property(nullable,nonatomic,copy) NSDictionary<NSString *,id> *titleTextAttributes NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
  44.  
  45. //标题垂直偏移
  46. - (void)setTitleVerticalPositionAdjustment:(CGFloat)adjustment forBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
  47.  
  48. - (CGFloat)titleVerticalPositionAdjustmentForBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
  49.  
  50. //设置返回按钮的图片
  51. @property(nullable,nonatomic,strong) UIImage *backIndicatorImage NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;
  52. @property(nullable,nonatomic,strong) UIImage *backIndicatorTransitionMaskImage NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;
  53.  
  54. @end

UINavigationBar是用于实现管理层级关系内容的组件,直接继承自UIView。通常用在UINavgationController类中,用于管理和显示UINavgationController的subViewController , 同时UINavgationBar也可以单独使用,添加至任何的UIView中。UINavigationBar比较重要的属性为,左侧按钮,中间的标题,以及右侧按钮。在平时的应用程序中,我们常常使用到自定义UINavigationBar来完成导航条的设置。

知识点1:导航栏全局属性设置

  1. //全局设置导航栏主题
  2. - (void)setNavigationControllerAppearance {
  3. [UINavigationBar appearance].barStyle = UIBarStyleBlack;
  4. [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithWhite:0.1 alpha:0.5]];
  5. [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
  6. }

全局设置导航栏的好处有两个:一是不用对每个NavigationBar进行设置;二是方便做主题管理,切换主题,只需要更改全局设置即可。

知识点2:自定义返回的图标并去除文字,要同时设置setBackIndicatorImage、setBackIndicatorTransitionMaskImage

  1. [self.navigationController.navigationBar setBackIndicatorImage:[UIImage imageNamed:@"navibar_back_btn_bg_normal.png"]];
  2.  
  3. [self.navigationController.navigationBar setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"navibar_back_btn_bg_normal.png"]];
  4.  
  5. UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleBordered target:nil action:nil];
  6.  
  7. self.navigationItem.backBarButtonItem = backItem;

知识点3:设置导航条底部线条的颜色

  1. 颜色转图片的代码:
  2.  
  3. @implementation UIImage (ColorImage)
  4.  
  5. + (UIImage *)imageWithColor:(UIColor *)color
  6. {
  7. CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
  8. UIGraphicsBeginImageContext(rect.size);
  9. CGContextRef context = UIGraphicsGetCurrentContext();
  10.  
  11. CGContextSetFillColorWithColor(context, [color CGColor]);
  12. CGContextFillRect(context, rect);
  13.  
  14. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  15. UIGraphicsEndImageContext();
  16.  
  17. return image;
  18. }
  19.  
  20. @end
  21.  
  22. 设置导航栏底部线条颜色的代码:
  23.  
  24. UINavigationBar *navigationBar = self.navigationController.navigationBar;
  25. [navigationBar setBackgroundImage:[[UIImage alloc] init]
  26. forBarPosition:UIBarPositionAny
  27. barMetrics:UIBarMetricsDefault];
  28. //此处使底部线条颜色为红色
  29. [navigationBar setShadowImage:[UIImage imageWithColor:[UIColor redColor]]];

知识点4:隐藏导航条底部线条

  1. //找查到Nav底部的黑线
  2. - (UIImageView *)findHairlineImageViewUnder:(UIView *)view
  3. {
  4. if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0)
  5. {
  6. return (UIImageView *)view;
  7. }
  8. for (UIView *subview in view.subviews) {
  9. UIImageView *imageView = [self findHairlineImageViewUnder:subview];
  10. if (imageView) {
  11. return imageView;
  12. }
  13. }
  14. return nil;
  15. }
  16.  
  17. 然后设置这个UIImageView为隐藏就可以了;

方法二:这种主要是隐藏后没办法再显示出来

  1. UINavigationBar *navigationBar = self.navigationController.navigationBar;
  2. //设置透明的背景图,便于识别底部线条有没有被隐藏
  3. [navigationBar setBackgroundImage:[[UIImage alloc] init]
  4. forBarPosition:UIBarPositionAny
  5. barMetrics:UIBarMetricsDefault];
  6. //此处使底部线条失效
  7. [navigationBar setShadowImage:[UIImage new]];

六:关于UINavigationItem的定义

  1. NS_CLASS_AVAILABLE_IOS(2_0) @interface UINavigationItem : NSObject <NSCoding>
  2.  
  3. - (instancetype)initWithTitle:(NSString *)title NS_DESIGNATED_INITIALIZER;
  4.  
  5. - (nullable instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER;
  6.  
  7. //设置导航栏中间的内容标题
  8. @property(nullable, nonatomic,copy) NSString *title;
  9. //设置导航栏中间的内容视图
  10. @property(nullable, nonatomic,strong) UIView *titleView;
  11.  
  12. @property(nullable,nonatomic,copy) NSString *prompt;
  13. //返回
  14. @property(nullable,nonatomic,strong) UIBarButtonItem *backBarButtonItem;
  15. //是否隐藏返回Button
  16. @property(nonatomic,assign) BOOL hidesBackButton;
  17.  
  18. - (void)setHidesBackButton:(BOOL)hidesBackButton animated:(BOOL)animated;
  19. //左边数组Item
  20. @property(nullable,nonatomic,copy) NSArray<UIBarButtonItem *> *leftBarButtonItems NS_AVAILABLE_IOS(5_0);
  21. //右边数组Item
  22. @property(nullable,nonatomic,copy) NSArray<UIBarButtonItem *> *rightBarButtonItems NS_AVAILABLE_IOS(5_0);
  23.  
  24. - (void)setLeftBarButtonItems:(nullable NSArray<UIBarButtonItem *> *)items animated:(BOOL)animated NS_AVAILABLE_IOS(5_0);
  25.  
  26. - (void)setRightBarButtonItems:(nullable NSArray<UIBarButtonItem *> *)items animated:(BOOL)animated NS_AVAILABLE_IOS(5_0);
  27.  
  28. //通过指定该属性为YES,可以让leftBarButtonItem和backBarButtonItem同时显示,其中leftBarButtonItem显示在backBarButtonItem的右边 默认值为NO
  29. @property(nonatomic) BOOL leftItemsSupplementBackButton NS_AVAILABLE_IOS(5_0);
  30. //左边Item
  31. @property(nullable, nonatomic,strong) UIBarButtonItem *leftBarButtonItem;
  32. //右边Item
  33. @property(nullable, nonatomic,strong) UIBarButtonItem *rightBarButtonItem;
  34.  
  35. - (void)setLeftBarButtonItem:(nullable UIBarButtonItem *)item animated:(BOOL)animated;
  36.  
  37. - (void)setRightBarButtonItem:(nullable UIBarButtonItem *)item animated:(BOOL)animated;
  38.  
  39. @end

UINavigationController会为每一个入栈的UIViewController生成一个UINavigationItem. UIViewController通过修改UINavigationItem可以控制UINavigationBar上的按钮和标题等

知识点1:关于UINavigationItem内容

  1. 你可以通过设置self.navigationItem.leftBarButtonItem为某个ButtonItem
  2. self.navigationItem.leftBarButtonItem
  3. self.navigationItem.rightBarButtonItem
  4. self.navigationItem.backBarButtonItem
  5. self.navigationItem.titleView等等
  6. 注:、这里的self 指的是UIViewController
  7. 、如果你在新视图中不修改backBarButtonItem leftBarButtonItem UINavigationController 会自动添加左边返回按钮用以返回了一个视图。总体的显示原则如下:
  8. )、Left side of the navigationBar 左按钮
  9. a)如果当前的viewController设置了leftBarButtonItem,则显示当前VC所自带的leftBarButtonItem
  10. b)如果当前的viewController没有设置leftBarButtonItem,且当前VC不是rootVC的时候,则显示前一层VCbackBarButtonItem。如果前一层的VC没有显示的指定backBarButtonItem的话,系统将会根据前一层VCtitle属性自动生成一个back按钮,并显示出来。
  11. c)如果当前的viewController没有设置leftBarButtonItem,且当前VC已是rootVC的时候,左边将不显示任何东西。
  12. 此处注意:.0中新增加了一个属性leftItemsSupplementBackButton,通过指定该属性为YES,可以让leftBarButtonItembackBarButtonItem同时显示,其中leftBarButtonItem显示在backBarButtonItem的右边。
  13. )、title 标题
  14. a)如果当前VC通过 .navigationItem.titleView指定了自定义的titleView,系统将会显示指定的titleView,此处要注意自定义titleView的高度不要超过navigationBar的高度,否则会显示出界。
  15. b)如果当前VC没有指定titleView,系统则会根据当前VCtitle或者当前VCnavigationItem.title的内容创建一个UILabel并显示,其中如果指定了navigationItem.title的话,则优先显示navigationItem.title的内容。
  16. )、Right side of the navigationBar  右按钮
  17. a)如果当前VC指定了rightBarButtonItem的话,则显示指定的内容。
  18. b)如果当前VC没有指定rightBarButtonItem的话,则不显示任何东西。

知识点2:相关属性运用

  1. NavigationBar设置中间的标题或者自定义View
  2. [self.navigationItem setTitle:@"旅行"];
  3. [self.navigationItem setTitleView:[UIImage imageNamed:@"图片名称"];
  4.  
  5. 单个或多个左右Item设置:单个leftItem设置:
  6. UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"BackIcon"] style:UIBarButtonItemStylePlain target:self action:nil];
  7. [leftBarButton setTintColor:[UIColor colorWithWhite: alpha:]];
  8. self.navigationItem.leftBarButtonItem = leftBarButton;
  9.  
  10. 多个rightItem设置:
  11. UIBarButtonItem *firstItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:nil];
  12. UIBarButtonItem *secondItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:nil];
  13. NSArray *rightItems = @[firstItem, secondItem];
  14. self.navigationItem.rightBarButtonItems = rightItems;

知识点3:设置偏移值

  1. // 返回按钮内容左靠
  2. button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  3. // 让返回按钮内容继续向左边偏移10
  4. button.contentEdgeInsets = UIEdgeInsetsMake(, -, , );
  5. viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

七:UIBarButtonItem定义内容

  1. NS_CLASS_AVAILABLE_IOS(2_0) @interface UIBarButtonItem : UIBarItem <NSCoding>
  2.  
  3. - (instancetype)init NS_DESIGNATED_INITIALIZER;
  4. //初始化
  5. - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
  6. - (instancetype)initWithImage:(nullable UIImage *)image style:(UIBarButtonItemStyle)style target:(nullable id)target action:(nullable SEL)action;
  7. - (instancetype)initWithImage:(nullable UIImage *)image landscapeImagePhone:(nullable UIImage *)landscapeImagePhone style:(UIBarButtonItemStyle)style target:(nullable id)target action:(nullable SEL)action NS_AVAILABLE_IOS(5_0);
  8. - (instancetype)initWithTitle:(nullable NSString *)title style:(UIBarButtonItemStyle)style target:(nullable id)target action:(nullable SEL)action;
  9. - (instancetype)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem target:(nullable id)target action:(nullable SEL)action;
  10. //自定义视图
  11. - (instancetype)initWithCustomView:(UIView *)customView;
  12.  
  13. //样式
  14. @property(nonatomic) UIBarButtonItemStyle style; // default is UIBarButtonItemStylePlain
  15. @property(nonatomic) CGFloat width;
  16. @property(nullable, nonatomic,copy) NSSet<NSString *> *possibleTitles; // default is nil
  17. //自定义视图
  18. @property(nullable, nonatomic,strong) __kindof UIView *customView;
  19. @property(nullable, nonatomic) SEL action; // default is NULL
  20. @property(nullable, nonatomic,weak) id target; // default is nil
  21.  
  22. - (void)setBackgroundImage:(nullable UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
  23. - (nullable UIImage *)backgroundImageForState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
  24.  
  25. - (void)setBackgroundImage:(nullable UIImage *)backgroundImage forState:(UIControlState)state style:(UIBarButtonItemStyle)style barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;
  26. - (nullable UIImage *)backgroundImageForState:(UIControlState)state style:(UIBarButtonItemStyle)style barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;
  27.  
  28. @property(nullable, nonatomic,strong) UIColor *tintColor NS_AVAILABLE_IOS(5_0);
  29.  
  30. - (void)setBackgroundVerticalPositionAdjustment:(CGFloat)adjustment forBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
  31. - (CGFloat)backgroundVerticalPositionAdjustmentForBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
  32.  
  33. - (void)setTitlePositionAdjustment:(UIOffset)adjustment forBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
  34. - (UIOffset)titlePositionAdjustmentForBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
  35.  
  36. - (void)setBackButtonBackgroundImage:(nullable UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
  37. - (nullable UIImage *)backButtonBackgroundImageForState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
  38.  
  39. - (void)setBackButtonTitlePositionAdjustment:(UIOffset)adjustment forBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
  40. - (UIOffset)backButtonTitlePositionAdjustmentForBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
  41.  
  42. - (void)setBackButtonBackgroundVerticalPositionAdjustment:(CGFloat)adjustment forBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
  43. - (CGFloat)backButtonBackgroundVerticalPositionAdjustmentForBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
  44.  
  45. @end

知识点1:UIBarButtonSystemItem的样式介绍

  1. typedef NS_ENUM(NSInteger, UIBarButtonSystemItem) {
  2. UIBarButtonSystemItemDone,//显示完成
  3. UIBarButtonSystemItemCancel,//显示取消
  4. UIBarButtonSystemItemEdit, //显示编辑
  5. UIBarButtonSystemItemSave, //显示保存
  6. UIBarButtonSystemItemAdd,//显示加号
  7. UIBarButtonSystemItemFlexibleSpace,//什么都不显示,占位一个空间位置
  8. UIBarButtonSystemItemFixedSpace,//和上一个类似
  9. UIBarButtonSystemItemCompose,//显示写入按钮
  10. UIBarButtonSystemItemReply,//显示循环按钮
  11. UIBarButtonSystemItemAction,//显示活动按钮
  12. UIBarButtonSystemItemOrganize,//显示组合按钮
  13. UIBarButtonSystemItemBookmarks,//显示图书按钮
  14. UIBarButtonSystemItemSearch,//显示查找按钮
  15. UIBarButtonSystemItemRefresh,//显示刷新按钮
  16. UIBarButtonSystemItemStop,//显示停止按钮
  17. UIBarButtonSystemItemCamera,//显示相机按钮
  18. UIBarButtonSystemItemTrash,//显示移除按钮
  19. UIBarButtonSystemItemPlay,//显示播放按钮
  20. UIBarButtonSystemItemPause,//显示暂停按钮
  21. UIBarButtonSystemItemRewind,//显示退后按钮
  22. UIBarButtonSystemItemFastForward,//显示前进按钮
  23. UIBarButtonSystemItemUndo,//显示消除按钮
  24. UIBarButtonSystemItemRedo ,//显示重做按钮
  25. UIBarButtonSystemItemPageCurl ,//在tool上有效
  26. };
  1. 例如:
  2.  
  3. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(right:)];
  4.  
  5. - (void)right:(id)sender
  6. {
  7. NSLog(@"rightBarButtonItem");
  8. }

知识点2:自定义UIView的UIBarButtonItem

  1. UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
  2. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:testView];

最近有个妹子弄的一个关于扩大眼界跟内含的订阅号,每天都会更新一些深度内容,在这里如果你感兴趣也可以关注一下(嘿对美女跟知识感兴趣),当然可以关注后输入:github 会有我的微信号,如果有问题你也可以在那找到我;当然不感兴趣无视此信息;

你真的了解UINavigationController吗?的更多相关文章

  1. UINavigationController(转)

    UINavigationController是IOS编程中比较常用的一种容器view controller,很多系统的控件(如UIImagePickerViewController)以及很多有名的AP ...

  2. UINavigationController使用详解

    UINavigationController使用详解 有一阵子没有写随笔,感觉有点儿手生.一个多月以后终于又一次坐下来静下心写随笔,记录自己的学习笔记,也希望能够帮到大家. 废话少说回到正题,UINa ...

  3. IOS开发之UINavigationController详解

    UINavigationController是IOS编程中比较常用的一种容器view controller,很多系统的控件(如UIImagePickerViewController)以及很多有名的AP ...

  4. iOS 之 UINavigationController 记录

    有一阵子没有写随笔,感觉有点儿手生.一个多月以后终于又一次坐下来静下心写随笔,记录自己的学习笔记,也希望能够帮到大家. 废话少说回到正题,UINavigationController是IOS编程中比较 ...

  5. UINavigationController详细(转)

    UINavigationController使用详解 有一阵子没有写随笔,感觉有点儿手生.一个多月以后终于又一次坐下来静下心写随笔,记录自己的学习笔记,也希望能够帮到大家. 废话少说回到正题,UINa ...

  6. App你真的需要么

    随着智能手机.移动路联网的普及,APP火的一塌糊涂,APP应用可谓五花八门,街上经常看到各种推广:扫码安装送东西,送优惠券.仿佛一夜之间一个企业没有自己的APP就跟不上时代了. 有时我在想:APP,你 ...

  7. [C#] C# 知识回顾 - 你真的懂异常(Exception)吗?

    你真的懂异常(Exception)吗? 目录 异常介绍 异常的特点 怎样使用异常 处理异常的 try-catch-finally 捕获异常的 Catch 块 释放资源的 Finally 块 一.异常介 ...

  8. 你真的会玩SQL吗?之逻辑查询处理阶段

    你真的会玩SQL吗?系列目录 你真的会玩SQL吗?之逻辑查询处理阶段 你真的会玩SQL吗?和平大使 内连接.外连接 你真的会玩SQL吗?三范式.数据完整性 你真的会玩SQL吗?查询指定节点及其所有父节 ...

  9. SQL Server中SELECT会真的阻塞SELECT吗?

    在SQL Server中,我们知道一个SELECT语句执行过程中只会申请一些意向共享锁(IS) 与共享锁(S), 例如我使用SQL Profile跟踪会话86执行SELECT * FROM dbo.T ...

随机推荐

  1. 关于IHttpModule的相关知识总结

    一.IHttpModule相关概述 using System; namespace System.Web { public interface IHttpModule { // 销毁不再被HttpMo ...

  2. 解决AndroidStudio升级版本后恢复初始化设置的问题

    今天把AndroidStudio升级到1.5后发现所有的个性设置全变为初始化了.包括皮肤啊,字体大小.颜色啊,以及快捷键等等.一瞬间就懵了. 升级完后好像有一个弹窗就是提示是否要继续使用之前的配置的, ...

  3. C#基础02

    学习"传智播客视频基础"做的课堂笔记,您有幸读到,若其中有错误部分,请您务必指明.另外请给出您的宝贵建议,谢谢. **************基础知识************ 1: ...

  4. JS打印页面指定区域

    错误的写法: //打印 function printPage(areaId) { if (parent.$("#PrinFrame").length == 0) { parent. ...

  5. MVC的路径查找顺序

    使用MVC的朋友们,知道MVC的funny之处. 但是如果出现路径找不到,请记住以下的页面路径寻找顺序. http://www.cnblogs.com/sosoft/ 首先,知道你的Controlle ...

  6. 一个不错的php验证码的类

    类的代码: <?php class Captcha { private $width; private $height; private $codeNum; private $code; pri ...

  7. 【UWP】UI适配整理

    做UWP有几个月了,期间发布了几个应用,在这里整理一下适配相关的一些东西,UWP关于UI的适配主要有两种方式: 1.VisualState+Trigger:通过触发器出发界面更变,通常在Desktop ...

  8. linq查询xml

    1.加载xml字符串 XElement root = XElement.Parse(@"<?xml version='1.0' encoding='utf-8'?> <It ...

  9. MVC中几种常用ActionResult

    一.定义 MVC中ActionResult是Action的返回结果.ActionResult 有多个派生类,每个子类功能均不同,并不是所有的子类都需要返回视图View,有些直接返回流,有些返回字符串等 ...

  10. Git tag 给当前分支打标签

    原文已经找不到出处,重新整理格式,仅作个人收藏! 标签(Tag)可以针对某一时间点的版本做标记,常用于版本发布. 列出tag $ git tag # 在控制台打印出当前仓库的所有tag $ git t ...