平时开发中,导航栏处理也是很重要的一环。也是具体开发前要先解决的问题之一
UINavigationController分析
UINavigationController的结构
UINavigationController.h分析
UINavigationController初始化
/**
* UINavigationController初始化
*
* @param navigationBarClass 自定义NavigationBar
* @param toolbarClass 自定义toolbar
*
* @return UINavigationController实例
*/
- (instancetype)initWithNavigationBarClass:(nullable Class)navigationBarClass toolbarClass:(nullable Class)toolbarClass NS_AVAILABLE_IOS(5_0);
/**
* UINavigationController初始化
*
* @param rootViewController 导航控制器的根控制器
*
* @return UINavigationController实例
*/
- (instancetype)initWithRootViewController:(UIViewController *)rootViewController; // Convenience method pushes the root view controller without animation.
UINavigationController的弹栈压栈
/**
* 压栈:将目标控制器压入栈中
*
* @param viewController 目标控制器
* @param animated 动画
*/
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated; // Uses a horizontal slide transition. Has no effect if the view controller is already in the stack.
/**
* 弹栈:将栈顶控制器从栈中弹出
*
* @param animated 动画
*
* @return 栈顶控制器
*/
- (nullable UIViewController *)popViewControllerAnimated:(BOOL)animated; // Returns the popped controller.
/**
* 弹栈:弹到指定的目标控制器
*
* @param viewController 目标控制器
* @param animated 动画
*
* @return 被弹出的视图控制器数组
*/
- (nullable NSArray<__kindof UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated; // Pops view controllers until the one specified is on top. Returns the popped controllers.
/**
* 弹栈:弹到根控制器
*
* @param animated 动画
*
* @return 被弹出的视图控制器数组
*/
- (nullable NSArray<__kindof UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated; // Pops until there's only a single view controller left on the stack. Returns the popped controllers.
/**
* 替换栈中的视图控制器数组
*
* @param viewControllers 视图控制器数组
* @param animated 动画
*/
- (void)setViewControllers:(NSArray<UIViewController *> *)viewControllers animated:(BOOL)animated NS_AVAILABLE_IOS(3_0); // If animated is YES, then simulate a push or pop depending on whether the new top view controller was previously in the stack.
其他属性和方法
visibleViewController 就是当前显示的控制器
topViewController 是某个导航栈的栈顶视图
visibleViewController和哪个导航栈没有关系,只是当前显示的控制器,也就是说任意一个导航的visibleViewController所返回的值应该是一样的,
但是topViewController 就是某个导航栈的栈顶视图,和导航嘻嘻相关
换句话说如果在仅有一个导航栈上,
visibleViewController和topViewController应该是没有区别得。
这段话引用于http://blog.sina.com.cn/s/blog_881ed8500102vo38.html
/**
* 栈顶控制器
*/
@property(nullable, nonatomic,readonly,strong) UIViewController *topViewController; // The top view controller on the stack.
/**
* visibleViewController
*/
@property(nullable, nonatomic,readonly,strong) UIViewController *visibleViewController; // Return modal view controller if it exists. Otherwise the top view controller.
/**
* 栈里的视图控制器数组
*/
@property(nonatomic,copy) NSArray<__kindof UIViewController *> *viewControllers; // The current view controller stack.
/**
* 是否隐藏导航栏
*/
@property(nonatomic,getter=isNavigationBarHidden) BOOL navigationBarHidden;
/**
* 设置导航栏隐藏
*
* @param hidden 是否隐藏
* @param animated 动画
*/
- (void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated; // Hide or show the navigation bar. If animated, it will transition vertically using UINavigationControllerHideShowBarDuration.
/**
* 导航栏
*/
@property(nonatomic,readonly) UINavigationBar *navigationBar; // The navigation bar managed by the controller. Pushing, popping or setting navigation items on a managed navigation bar is not supported.
/**
* toolbar是否隐藏
*/
@property(nonatomic,getter=isToolbarHidden) BOOL toolbarHidden NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED; // Defaults to YES, i.e. hidden.
/**
* 设置toolbar是否隐藏
*
* @param hidden 是否隐藏
* @param animated 动画
*/
- (void)setToolbarHidden:(BOOL)hidden animated:(BOOL)animated NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED; // Hide or show the toolbar at the bottom of the screen. If animated, it will transition vertically using UINavigationControllerHideShowBarDuration.
/**
* toolbar
*/
@property(null_resettable,nonatomic,readonly) UIToolbar *toolbar NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED; // For use when presenting an action sheet.
/**
* 代理
*/
@property(nullable, nonatomic, weak) id<UINavigationControllerDelegate> delegate;
/**
* 边缘侧滑返回手势
*/
@property(nullable, nonatomic, readonly) UIGestureRecognizer *interactivePopGestureRecognizer NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
/**
* 展示视图控制器
*
* @param vc 视图控制器
* @param sender <#sender description#>
*/
- (void)showViewController:(UIViewController *)vc sender:(nullable id)sender NS_AVAILABLE_IOS(8_0); // Interpreted as pushViewController:animated:
/// When the keyboard appears, the navigation controller's navigationBar toolbar will be hidden. The bars will remain hidden when the keyboard dismisses, but a tap in the content area will show them.键盘弹出的时候隐藏导航栏,键盘隐藏时导航栏仍然保持着隐藏状态,但是当点击视图区域的时候回出现
@property (nonatomic, readwrite, assign) BOOL hidesBarsWhenKeyboardAppears NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;
/// When the user swipes, the navigation controller's navigationBar & toolbar will be hidden (on a swipe up) or shown (on a swipe down). The toolbar only participates if it has items. 滑动隐藏导航栏
@property (nonatomic, readwrite, assign) BOOL hidesBarsOnSwipe NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;
/// The gesture recognizer that triggers if the bars will hide or show due to a swipe. Do not change the delegate or attempt to replace this gesture by overriding this method.滑动隐藏导航栏手势
@property (nonatomic, readonly, strong) UIPanGestureRecognizer *barHideOnSwipeGestureRecognizer NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;
/// When the UINavigationController's vertical size class is compact, hide the UINavigationBar and UIToolbar. Unhandled taps in the regions that would normally be occupied by these bars will reveal the bars.
@property (nonatomic, readwrite, assign) BOOL hidesBarsWhenVerticallyCompact NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;
/// When the user taps, the navigation controller's navigationBar & toolbar will be hidden or shown, depending on the hidden state of the navigationBar. The toolbar will only be shown if it has items to display.点击隐藏导航栏
@property (nonatomic, readwrite, assign) BOOL hidesBarsOnTap NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;
/// The gesture recognizer used to recognize if the bars will hide or show due to a tap in content. Do not change the delegate or attempt to replace this gesture by overriding this method.点击隐藏导航栏手势
@property (nonatomic, readonly, assign) UITapGestureRecognizer *barHideOnTapGestureRecognizer NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;
UINavigationController的delegate
UINavigationController的delegate方法我平时的开发工作中很少用到
/**
* 将要显示目标控制器
*
* @param navigationController 当前导航控制器
* @param viewController 目标视图控制器
* @param animated 动画
*/
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
/**
* 目标控制器最终显示
*
* @param navigationController 当前导航控制器
* @param viewController 目标视图控制器
* @param animated 动画
*/
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
/**
* 横竖屏切换
*
* @param navigationController 当前导航控制器
*
* @return 横竖屏方向
*/
- (UIInterfaceOrientationMask)navigationControllerSupportedInterfaceOrientations:(UINavigationController *)navigationController NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
/**
* 横竖屏切换
*
* @param navigationController 当前导航控制器
*
* @return 横竖屏方向
*/
- (UIInterfaceOrientation)navigationControllerPreferredInterfaceOrientationForPresentation:(UINavigationController *)navigationController NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
/**
* 转场动画
*
* @param navigationController 当前导航控制器
* @param animationController 动画控制器
*
* @return 转场动画
*/
- (nullable id <UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController
interactionControllerForAnimationController:(id <UIViewControllerAnimatedTransitioning>) animationController NS_AVAILABLE_IOS(7_0);
/**
* 转场动画
*
* @param navigationController 当前导航控制器
* @param operation 动画类型枚举
* @param fromVC 起始视图控制器
* @param toVC 目标视图控制器
*
* @return 转场动画
*/
- (nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
animationControllerForOperation:(UINavigationControllerOperation)operation
fromViewController:(UIViewController *)fromVC
toViewController:(UIViewController *)toVC NS_AVAILABLE_IOS(7_0);
ViewController分类拓展item属性等
@interface UIViewController (UINavigationControllerItem)
/**
* 导航栏上面用户自定义视图
*/
@property(nonatomic,readonly,strong) UINavigationItem *navigationItem; // Created on-demand so that a view controller may customize its navigation appearance.
/**
* 推送时隐藏bottom
*/
@property(nonatomic) BOOL hidesBottomBarWhenPushed __TVOS_PROHIBITED; // If YES, then when this view controller is pushed into a controller hierarchy with a bottom bar (like a tab bar), the bottom bar will slide out. Default is NO.
/**
* 下级视图的导航控制器
*/
@property(nullable, nonatomic,readonly,strong) UINavigationController *navigationController; // If this view controller has been pushed onto a navigation controller, return it.
@end
ViewController分类拓展toolbarItems属性等
@interface UIViewController (UINavigationControllerContextualToolbarItems)
@property (nullable, nonatomic, strong) NSArray<__kindof UIBarButtonItem *> *toolbarItems NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED;
- (void)setToolbarItems:(nullable NSArray<UIBarButtonItem *> *)toolbarItems animated:(BOOL)animated NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED;
@end
UINavigationItem分析
UINavigationItem是导航栏控制的主要类,我们自定义title,返回按钮,rightItem等等都是从它下手
/**
* 初始化
*
* @param title 标题
*
* @return 初始化
*/
- (instancetype)initWithTitle:(NSString *)title NS_DESIGNATED_INITIALIZER;
/**
* xib初始化
*
* @param coder
*
* @return 初始化
*/
- (nullable instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER;
/**
* title
*/
@property(nullable, nonatomic,copy) NSString *title; // Title when topmost on the stack. default is nil
/**
* 自定义titleView
*/
@property(nullable, nonatomic,strong) UIView *titleView; // Custom view to use in lieu of a title. May be sized horizontally. Only used when item is topmost on the stack.
@property(nullable,nonatomic,copy) NSString *prompt __TVOS_PROHIBITED; // Explanatory text to display above the navigation bar buttons.
/**
* 返回按钮
*/
@property(nullable,nonatomic,strong) UIBarButtonItem *backBarButtonItem __TVOS_PROHIBITED; // Bar button item to use for the back button in the child navigation item.
/**
* 隐藏返回按钮
*/
@property(nonatomic,assign) BOOL hidesBackButton __TVOS_PROHIBITED; // If YES, this navigation item will hide the back button when it's on top of the stack.
- (void)setHidesBackButton:(BOOL)hidesBackButton animated:(BOOL)animated __TVOS_PROHIBITED;
/**
* 左边item数组
*/
@property(nullable,nonatomic,copy) NSArray<UIBarButtonItem *> *leftBarButtonItems NS_AVAILABLE_IOS(5_0);
/**
* 右边item数组
*/
@property(nullable,nonatomic,copy) NSArray<UIBarButtonItem *> *rightBarButtonItems NS_AVAILABLE_IOS(5_0);
- (void)setLeftBarButtonItems:(nullable NSArray<UIBarButtonItem *> *)items animated:(BOOL)animated NS_AVAILABLE_IOS(5_0);
- (void)setRightBarButtonItems:(nullable NSArray<UIBarButtonItem *> *)items animated:(BOOL)animated NS_AVAILABLE_IOS(5_0);
/* By default, the leftItemsSupplementBackButton property is NO. In this case,
the back button is not drawn and the left item or items replace it. If you
would like the left items to appear in addition to the back button (as opposed to instead of it)
set leftItemsSupplementBackButton to YES.
*/
@property(nonatomic) BOOL leftItemsSupplementBackButton NS_AVAILABLE_IOS(5_0) __TVOS_PROHIBITED;
/**
* 左边item
*/
@property(nullable, nonatomic,strong) UIBarButtonItem *leftBarButtonItem;
/**
* 右边item
*/
@property(nullable, nonatomic,strong) UIBarButtonItem *rightBarButtonItem;
- (void)setLeftBarButtonItem:(nullable UIBarButtonItem *)item animated:(BOOL)animated;
- (void)setRightBarButtonItem:(nullable UIBarButtonItem *)item animated:(BOOL)animated;
iOS开发中导航栏实际用到的情景
-
设置导航栏字体属性
-
设置item字体颜色
-
设置导航栏背景图片
. 设置导航栏背景颜色
. 设置导航栏背景透明
-
自定义导航栏返回按钮