您的位置:首页 > 产品设计 > UI/UE

【IOS 开发学习总结-OC-54】★★ios开发UI 控件——UINavigationBar 与UINavigationController

2015-10-18 16:17 666 查看

【IOS 开发学习总结-OC-54】★★ios开发UI 控件——UINavigationBar 与UINavigationController

UINavigationController是个控制器,用户交互行为主要靠UINavigationBar来完成。

UINavigationBar

UINavigationBar——通常位于屏幕顶端,继承了 UIView 控件,通常只是作为多个UINavigationItem 的容器。它通过 stack (栈)的形式管理多个UINavigationItem,所以每次UINavigationBar上只能看到一个UINavigationItem对象。 它们二者的关系如下图:



UINavigationBar中的属性和方法有:

NS_CLASS_AVAILABLE_IOS(2_0) @interface UINavigationBar : UIView <NSCoding, UIBarPositioning>

@property(nonatomic,assign) UIBarStyle barStyle;
@property(nullable,nonatomic,weak) id<UINavigationBarDelegate> delegate;

@property(nonatomic,assign,getter=isTranslucent) BOOL translucent NS_AVAILABLE_IOS(3_0) UI_APPEARANCE_SELECTOR; // Default is NO on iOS 6 and earlier. Always YES if barStyle is set to UIBarStyleBlackTranslucent(黑色透明)

// Pushing a navigation item displays the item's title in the center of the navigation bar.
// The previous top navigation item (if it exists) is displayed as a "back" button on the left.
- (void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated;
//将一个pushNavigationItem压入UINavigationBar 的栈中。

- (nullable UINavigationItem *)popNavigationItemAnimated:(BOOL)animated;
//将UINavigationBar栈顶的UINavigationItem弹出
// Returns the item that was popped.

@property(nullable, nonatomic,readonly,strong) UINavigationItem *topItem;
//返回最顶层的UINavigationItem控件

@property(nullable, nonatomic,readonly,strong) UINavigationItem *backItem;
//返回UINavigationItem控件最顶层下面的UINavigationItem控件

@property(nullable,nonatomic,copy) NSArray<UINavigationItem *> *items;
//返回UINavigationBar包含的多个UINavigationItem控件

- (void)setItems:(nullable NSArray<UINavigationItem *> *)items animated:(BOOL)animated;
// 同时为UINavigationBar设置多个UINavigationItem控件
// If animated is YES, then simulate a push or pop depending on whether the new top item was previously in the stack.

@property(null_resettable, nonatomic,strong) UIColor *tintColor;//着色

@property(nullable, nonatomic,strong) UIColor *barTintColor NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;  //导航栏着色  default is nil

- (void)setBackgroundImage:(nullable UIImage *)backgroundImage forBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;
//通过导航栏位置和量度设置背景图片 官方注解:Sets the background image to use for a given bar position and set of metrics(指标,度量).

- (nullable UIImage *)backgroundImageForBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;
//通过导航栏位置和量度获取背景图片

- (void)setBackgroundImage:(nullable UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
//通过导航栏量度设置背景图片

- (nullable UIImage *)backgroundImageForBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
//通过导航栏量度获取背景图片

@property(nullable, nonatomic,strong) UIImage *shadowImage NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;//阴影图片

@property(nullable,nonatomic,copy) NSDictionary<NSString *,id> *titleTextAttributes NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;//标题文本属性

- (void)setTitleVerticalPositionAdjustment:(CGFloat)adjustment forBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
- (CGFloat)titleVerticalPositionAdjustmentForBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
//调整标题垂直位置

@property(nullable,nonatomic,strong) UIImage *backIndicatorImage NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;
//背景指示器图片

@property(nullable,nonatomic,strong) UIImage *backIndicatorTransitionMaskImage NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;
//The image used as a mask for content during push and pop transitions.——作为push and pop过渡的遮罩图片

@end


UINavigationItem的属性和方法:

NS_CLASS_AVAILABLE_IOS(2_0) @interface UINavigationItem : NSObject <NSCoding>

- (instancetype)initWithTitle:(NSString *)title NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER;

@property(nullable, nonatomic,copy)   NSString        *title;
//UINavigationItem的标题文本—— Title when topmost on the stack. default is nil

@property(nullable, nonatomic,strong) UIView          *titleView;
//UINavigationItem的标题视图—— 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;     //提示内容  Explanatory text to display above the navigation bar buttons.

@property(nullable,nonatomic,strong) UIBarButtonItem *backBarButtonItem;
//返回按钮  Bar button item to use for the back button in the child navigation item.

@property(nonatomic,assign) BOOL hidesBackButton;
//是否隐藏返回按钮   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;
//设置隐藏返回按钮的方法

@property(nullable,nonatomic,copy) NSArray<UIBarButtonItem *> *leftBarButtonItems NS_AVAILABLE_IOS(5_0);
@property(nullable,nonatomic,copy) NSArray<UIBarButtonItem *> *rightBarButtonItems NS_AVAILABLE_IOS(5_0);
//获取UIBarButtonItem左边/右边的多个按钮组成的数组

- (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);
//设置UIBarButtonItem左边/右边的多个按钮组成的数组

@property(nonatomic) BOOL leftItemsSupplementBackButton NS_AVAILABLE_IOS(5_0);
//A Boolean value indicating whether the left items are displayed in addition to(除...之外) the back button.

@property(nullable, nonatomic,strong) UIBarButtonItem *leftBarButtonItem;
@property(nullable, nonatomic,strong) UIBarButtonItem *rightBarButtonItem;
//获取左/右边的UIBarButtonItem

- (void)setLeftBarButtonItem:(nullable UIBarButtonItem *)item animated:(BOOL)animated;
- (void)setRightBarButtonItem:(nullable UIBarButtonItem *)item animated:(BOOL)animated;
//设置左/右边的UIBarButtonItem
@end


UINavigationBarDelegate中的方法

@protocol UINavigationBarDelegate <UIBarPositioningDelegate>

@optional

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item; // called to push. return NO not to.

- (void)navigationBar:(UINavigationBar *)navigationBar didPushItem:(UINavigationItem *)item;    // called at end of animation of push or immediately if not animated

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item;  // same as push methods

- (void)navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item;

@end


示例:



实现代码:

#import "FKViewController.h"

@interface FKViewController ()

@end

@implementation FKViewController
// 记录当前是添加第几个UINavigationItem的计数器
NSInteger count;
UINavigationBar *navigationBar;
- (void)viewDidLoad
{
[super viewDidLoad];
count = 1;
// 创建一个导航栏
navigationBar = [[UINavigationBar alloc]
initWithFrame:CGRectMake(0, 20, 320, 44)];
// 把导航栏添加到视图中
[self.view addSubview:navigationBar];
// 调用push方法添加一个UINavigationItem
[self push];
}

-(void)push
{
// 把导航栏集合添加入导航栏中,设置动画打开
[navigationBar pushNavigationItem:
[self makeNavItem] animated:YES];
count++;
}

-(void)pop
{
// 如果还有超过2个的UINavigationItem
if(count > 2)
{
count--;
// 弹出最顶层的UINavigationItem
[navigationBar popNavigationItemAnimated:YES];
}
else
{
// 使用UIAlertView提示用户
UIAlertView* alert = [[UIAlertView alloc]
initWithTitle:@"提示"
message:@"只剩下最后一个导航项,再出栈就没有了"
delegate:nil cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
}
}

- (UINavigationItem*) makeNavItem
{
// 创建一个导航项
UINavigationItem *navigationItem = [[UINavigationItem alloc]init];
// 创建一个左边按钮
UIBarButtonItem *leftButton =[[UIBarButtonItem alloc]initWithTitle:@"push" style:UIBarButtonItemStyleBordered target:self action:@selector(push)];
// 创建一个右边按钮
UIBarButtonItem *rightButton =[[UIBarButtonItem alloc]initWithTitle:@"pop" style:UIBarButtonItemStyleBordered target:self action:@selector(pop)];
//设置导航栏内容
navigationItem.title = [NSString stringWithFormat:
@"第【%ld】个导航项"
, (long)count];
//把左右两个按钮添加入导航栏集合中
[navigationItem setLeftBarButtonItem:leftButton];
[navigationItem setRightBarButtonItem:rightButton];
return navigationItem;
}
@end


UINavigationItem UINavigationBar 等的关系分析

这里简单概括下它们的关系:

UIBarItem-> UIBarButtonItem -> UINavigationItem—> UINavigationBar—>UINavigtionController

UIBarItem:

UIBarItem类是一个可以放置在Bar之上的所有小控件类的抽象类。继承了该基类所有子类在外观上类似于一个Button,它们都有一个标题,图片,动作以及目标,这点可以从其子类的初始化方法处看到。

UIBarButtonItem:

专门用来放在UIToolbar 或者 UINavigationBar的特殊button.基本行为跟button是一样的。另外从IOS5开始的Customizing Appearance 增加了一系列的方法调整显示。

UINavigationItem: NSObject

(包含了当前页面导航栏上需要显示的全部信息)title,prompt,titleView,leftBarButtonItem,rightBarButtonItem,backBarButonItem

UINavigationBar :UIView

NavigaitonBar就是导航栏,位于屏幕的上方,管理整个NavigationController的navigationItem,即类似navigationcontroller一样提供了一个栈来管理item。

UINavigtionController :UIViewController

包含:viewcontrollers、navigationbar、toolbar

更多内容可以参考文章:UINavigationItem UINavigationBar 关系分析

UINavigationController

UINavigationController通过栈的方式管理多个 UIViewController(每个控制器控制一个界面)。

如下示意图:



UINavigationController,封装了navigationbar,并且会自动给每个受它管理的viewcontroller自动添加navigationbar。

UINavigationController封装的控件如下图(新旧版本对比,后为新):





UINavigationController操作其管理的 UIViewController 的属性和方法

NS_CLASS_AVAILABLE_IOS(2_0) @interface UINavigationController : UIViewController

- (instancetype)initWithNavigationBarClass:(nullable Class)navigationBarClass toolbarClass:(nullable Class)toolbarClass NS_AVAILABLE_IOS(5_0);
//初始化

- (instancetype)initWithRootViewController:(UIViewController *)rootViewController;
//通过根视图初始化 Convenience method pushes the root view controller without animation.

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;
//压入视图控制器 Uses a horizontal slide transition. Has no effect if the view controller is already in the stack.

- (nullable UIViewController *)popViewControllerAnimated:(BOOL)animated;
//弹出栈顶的视图控制器 Returns the popped controller.

- (nullable NSArray<__kindof UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated;
//弹出到指定的视图控制器  Pops view controllers until the one specified is on top. Returns the popped controllers.

- (nullable NSArray<__kindof UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated;
//弹出除根视图控制器外的所有控制器  Pops until there's only a single view controller left on the stack. Returns the popped controllers.

@property(nullable, nonatomic,readonly,strong) UIViewController *topViewController;
//最上面的视图控制器  The top view controller on the stack.

@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;
//当前UINavigationController管理栈里的所有视图控制器  The current view controller stack.

- (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.

@property(nonatomic,getter=isNavigationBarHidden) BOOL navigationBarHidden;
//是否隐藏导航栏

- (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.

@property(nonatomic,getter=isToolbarHidden) BOOL toolbarHidden NS_AVAILABLE_IOS(3_0);
//是否隐藏工具栏  Defaults to YES, i.e. hidden.

- (void)setToolbarHidden:(BOOL)hidden animated:(BOOL)animated NS_AVAILABLE_IOS(3_0);
//隐藏工具栏的方法   Hide or show the toolbar at the bottom of the screen. If animated, it will transition vertically using UINavigationControllerHideShowBarDuration.

@property(null_resettable,nonatomic,readonly) UIToolbar *toolbar NS_AVAILABLE_IOS(3_0);
//返回UINavigationController管理的工具条 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);
//The gesture recognizer responsible for(为...负责) popping the top view controller off the navigation stack. (read-only)

- (void)showViewController:(UIViewController *)vc sender:(nullable id)sender NS_AVAILABLE_IOS(8_0);
// Interpreted(解释) as pushViewController:animated:

@property (nonatomic, readwrite, assign) BOOL hidesBarsWhenKeyboardAppears NS_AVAILABLE_IOS(8_0);
//键盘出现时是否隐藏栏

@property (nonatomic, readwrite, assign) BOOL hidesBarsOnSwipe NS_AVAILABLE_IOS(8_0);
//猛击时是否隐藏栏

@property (nonatomic, readonly, strong) UIPanGestureRecognizer *barHideOnSwipeGestureRecognizer NS_AVAILABLE_IOS(8_0);
//拖拽手势  The gesture recognizer used to hide the navigation bar and toolbar. (read-only)

@property (nonatomic, readwrite, assign) BOOL hidesBarsWhenVerticallyCompact NS_AVAILABLE_IOS(8_0);
//A Boolean value indicating whether the navigation controller hides its bars in a vertically compact(紧凑的,简洁的) environment.

@property (nonatomic, readwrite, assign) BOOL hidesBarsOnTap NS_AVAILABLE_IOS(8_0);
//轻击时是否隐藏

@property (nonatomic, readonly, assign) UITapGestureRecognizer *barHideOnTapGestureRecognizer NS_AVAILABLE_IOS(8_0);
//轻击手势  The gesture recognizer used to hide and show the navigation and toolbar. (read-only)

@end


怎样在UINavigationController底部添加工具条?

在UINavigationController底部添加工具条,直接调用受UINavigationController管理的 UIViewController 的
setToolBarItems:animated:
方法——就可以为不同视图控制器分别添加不同的工具条。

并不是通过对 ① 标识的对 toolBar 属性赋值来实现。

UINavigationController 示例——查看,编辑职业说明:



示例代码下载地址:

UINavigationController示例——查看,编辑职业说明
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息