UINavigationBar

一、简介

<

<

<<继承关系:UINavigationBar --> UIView -->UIResponder-->NSObject

格式为

1--> 设置UINavigationBar的样式(属性的作用)

typedef NS_ENUM(NSInteger, UIBarStyle) {

    UIBarStyleDefault          = 0,

    UIBarStyleBlack            = 1,

    UIBarStyleBlackOpaque      = 1, // Deprecated. Use UIBarStyleBlack

    UIBarStyleBlackTranslucent = 2, // Deprecated. Use UIBarStyleBlack and set the translucent property to YES

} __TVOS_PROHIBITED;

;(如果属性有枚举类型的话,这里会有枚举类型说明)

navigationBar.barStyle =UIBarStyleBlack;(这是具体的例子)

@property(nonatomic) UIBarStyle barStyle UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED; (这是属性的说明)

二、UINavigationBar的属性(属性的顺序与苹果API一致)

1--> 设置UINavigationBar的样式

typedef NS_ENUM(NSInteger, UIBarStyle) {

    UIBarStyleDefault          = 0,//默认样式

    UIBarStyleBlack            = 1,//黑色

    UIBarStyleBlackOpaque      = 1, // 弃用属性

    UIBarStyleBlackTranslucent = 2, // 弃用属性

} __TVOS_PROHIBITED;

toolBar.barStyle =UIBarStyleBlack;

@property(nonatomic) UIBarStyle barStyle UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED; 

2-->声明UINavigationBarDelegate代理

navigationBar .delegate = self;//声明代理

@property(nullable, nonatomic, weak) id delegate; //设置UINavigationBarDelegate代理

3-->设置UINavigationBar是否透明

navigationBar.translucent =NO;

@property(nonatomic,assign,getter=isTranslucent) BOOL translucent NS_AVAILABLE_IOS(3_0) UI_APPEARANCE_SELECTOR; //iOS 6和之前,默认情况下为NO。如果barStyle将UIBarStyleBlackTranslucent总是YES。

4、将新的UINavigationItem 压入栈

[navigationBar pushNavigationItem:item animated:NO];

- (void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated;

4、将顶部的UINavigationItem 推出栈

[navigationBar pushNavigationItem:item animated:NO];

- (nullable UINavigationItem *)popNavigationItemAnimated:(BOOL)animated; //

返回已弹出的项。

5、当前push到最上层的item

  UINavigationItem *theTop=[navigationBar topItem];

@property(nullable, nonatomic,readonly,strong) UINavigationItem *topItem;//只读属性

6、仅次于最上层的item,一般式被推向导航栏左侧的item

  UINavigationItem *theTop=[navigationBar backItem];

@property(nullable, nonatomic,readonly,strong) UINavigationItem *backItem;//只读属性

7、获取堆栈中所有item的数组

  NSArray *array=[navigationBar items];

@property(nullable,nonatomic,copy) NSArray *items;

8、设置一组item

    [navigationBar setItems:items animated:NO];  

- (void)setItems:(nullable NSArray *)items animated:(BOOL)animated;// 如果动画是YES,则模拟一个push或pop,这取决于新的top项目是否在堆栈中。

9、是否显示大标题

self.navigationController.navigationBar.prefersLargeTitles=YES;//这句话表示是否显示大标题

@property (nonatomic, readwrite, assign) BOOL prefersLargeTitles;

10、设置渲染颜色,会影响选中字体和图案的渲染

navigationBar.tintColor=[UIColor redColor];

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

11、设置导航栏的颜色

navigationBar.barTintColor = [UIColor whiteColor];

@property(nullable, nonatomic, strong) UIColor *barTintColor NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;// barTintColor会影响背景颜色, iOS7出现的新属性,用来代替tintColor的作用

12、通过导航栏位置和量度设置背景图片

//设置透明的背景图,便于识别底部线条有没有被隐藏

[navigationBar setBackgroundImage:[[UIImage alloc] init] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];

- (void)setBackgroundImage:(nullable UIImage *)backgroundImage forBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;

13、通过导航栏位置和量度获取背景图片

UIImage *image=[navBar backgroundImageForBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];

- (nullable UIImage *)backgroundImageForBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;

14、通过导航栏量度设置背景图片

[navigationBar setBackgroundImage:[[UIImage alloc] init] for BarMetrics:UIBarMetricsDefault];

- (void)setBackgroundImage:(nullable UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

15、通过导航栏量度获取背景图片

 UIImage *image=[navBar backgroundImageForBarMetrics:UIBarMetricsDefault];

- (nullable UIImage *)backgroundImageForBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

16、设置导航栏的阴影图片

[navigationBar setShadowImage:[UIImage new]];

@property(nullable, nonatomic,strong) UIImage *shadowImage NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;

17、设置导航栏的标题字体属性

navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor redColor]};

@property(nullable,nonatomic,copy) NSDictionary *titleTextAttributes NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

18、设置大标题的文本样式

[self.navigationController.navigationBar setLargeTitleTextAttributes:[NSDictionarydictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName,[UIFontsystemFontOfSize:18.0f],NSFontAttributeName,nil]];

@property(nullable, nonatomic, copy) NSDictionary *largeTitleTextAttributes UI_APPEARANCE_SELECTOR API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos);

19、设置导航栏标题的竖直位置偏移

[navBar setTitleVerticalPositionAdjustment:10 forBarMetrics:UIBarMetricsDefault];//adjustment指定了偏移量,正值为向下偏移,负值为向上偏移,如设置当前title向下偏移10个像素

- (void)setTitleVerticalPositionAdjustment:(CGFloat)adjustment forBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

20、获取导航栏标题的竖直位置偏移

CGFloat adjust=[navBar titleVerticalPositionAdjustmentForBarMetrics:UIBarMetricsDefault];

- (CGFloat)titleVerticalPositionAdjustmentForBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

21、设置背景指示器图片(返回按钮的样式,这两个一起设置)

[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@"nav_back.png"]];

@property(nullable,nonatomic,strong) UIImage *backIndicatorImage NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED;//导航栏左侧pop按钮的图案默认是一个箭头,我们可以使用这个的方法修改

22、设置背景指示器图片(返回按钮的样式,这两个一起设置)

[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"nav_back.png"]];

@property(nullable,nonatomic,strong) UIImage *backIndicatorTransitionMaskImage NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED;// 通常你说的这两个方法设置的图片应该是一样的, 除非你想让backbutton在点击后的效果有所不同。

三、UINavigationBar的代理属性

1-->item将要push的时候调用,返回NO,则不能push

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item{

return NO;

}

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item; 

2-->item已经push后调用

- (void)navigationBar:(UINavigationBar *)navigationBar didPushItem:(UINavigationItem *)item{

}

- (void)navigationBar:(UINavigationBar *)navigationBar didPushItem:(UINavigationItem *)item; // 在动画结束时被调用

3-> item将要pop时调用,返回NO,不能pop

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item{

return NO;

}

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item; 

4-> item已经pop后调用

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

}

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

参考

UINavigationBar 一个有样式的view

ios开发UI 控件——UINavigationBar 与UINavigationController

navigationBar

UInavigationBar上添加左箭头的返回按钮

iOS开发UINavigation——导航控制器UINavigationController

你可能感兴趣的:(UINavigationBar)