One, navigation bar Uinavigationbar
1, the use of navigation bar
In iOS development, we usually use the navigation controller, the navigation controller encapsulates a uinavigationbar, in fact, we can also without using the navigation controller in the premise of the use of the navigation bar alone, in Uinavigationbar, there are many we can customize the properties , it is very convenient to use.
2, the creation of Uinavigationbar and style type
The navigation bar inherits from UIView, so we can create a navigation bar like a normal view, such as creating a navigation bar with a height of 80, placing it in the head of the Viewcontroller, as follows:
UINavigationBar *bar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 80)];
[self.view addSubview:bar];
The effect is as follows:
We can also set the style properties of the navigation bar, from the iOS6, the Uinavigationbar defaults to the translucent style, from above also can be seen, the white navigation bar below a little background red. The style properties of the navigation bar can be set by using the following properties:
@property(nonatomic,assign) UIBarStyle barStyle;
Uibarstyle is an enumeration in which most of the styles have been deprecated, with only the following two effects:
typedef NS_ENUM(NSInteger, UIBarStyle) {
UIBarStyleDefault = 0,//默认
UIBarStyleBlack = 1,//黑色
}
The default style is the white style we see above, the black style effect is as follows:
3, navigation bar common properties and methods
From the above we can see that the IOS6 navigation bar by default is translucent, we can set this property by the bool value below, set to No, the navigation bar is not transparent, the default is yes:
@property(nonatomic,assign,getter=isTranslucent) BOOL translucent;
The following methods are used to set the color-dependent properties of the Navigationbar and the item above:
@property(null_resettable, nonatomic,strong) UIColor *tintColor;
Tintcolor This property affects the pattern color and font color of the pop button on the left side of the navigation bar, and the system defaults to the following colors:
@property(nullable, nonatomic,strong) UIColor *barTintColor;
Bartintcolor is used to set the background color of the navigation bar, and the translucent effect is invalidated when this property is set:
- (void)setBackgroundImage:(nullable UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
- (nullable UIImage *)backgroundImageForBarMetrics:(UIBarMetrics)barMetrics;
The above two methods are used to set and get the background pattern of the navigation bar, it should be noted that the default background pattern is not to do scaling, so we use the picture size and navigation bar size matching, which also has a uibarmetrics parameter, this parameter sets the status of the device, as follows:
typedef NS_ENUM(NSInteger, UIBarMetrics) {
UIBarMetricsDefault,//正常竖屏状态
UIBarMetricsCompact,//横屏状态
};
//设置导航栏的阴影图片
@property(nullable, nonatomic,strong) UIImage *shadowImage;
//设置导航栏的标题字体属性
@property(nullable,nonatomic,copy) NSDictionary<NSString *,id> *titleTextAttributes;
The caption font properties affect the middle caption of the navigation bar, as follows:
bar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor redColor]};
We can also set the vertical position offset of the navigation bar title by using the following properties:
- (void)setTitleVerticalPositionAdjustment:(CGFloat)adjustment forBarMetrics:(UIBarMetrics)barMetrics;
- (CGFloat)titleVerticalPositionAdjustmentForBarMetrics:(UIBarMetrics)barMetrics;
There is also a detail, navigation bar left pop button pattern by default is an arrow, we can use the following method to modify:
@property(nullable,nonatomic,strong) UIImage *backIndicatorImage;
@property(nullable,nonatomic,strong) UIImage *backIndicatorTransitionMaskImage;
4, the navigation bar of the item push and pop operation
Uinavigationbar above is not just a simple display title, it also has the title of the stack management, each title abstract object in the iOS system is Uinavigationitem object, we can through the push and pop operations to manage the item group.
//向栈中添加一个item,上一个item会被推向导航栏的左侧,变为pop按钮,会有一个动画效果
- (void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated;
//pop一个item
- (nullable UINavigationItem *)popNavigationItemAnimated:(BOOL)animated;
//当前push到最上层的item
@property(nullable, nonatomic,readonly,strong) UINavigationItem *topItem;
//仅次于最上层的item,一般式被推向导航栏左侧的item
@property(nullable, nonatomic,readonly,strong) UINavigationItem *backItem;
//获取堆栈中所有item的数组
@property(nullable,nonatomic,copy) NSArray<UINavigationItem *> *items;
//设置一组item
- (void)setItems:(nullable NSArray<UINavigationItem *> *)items animated:(BOOL)animated;
5, Uinavigationbardelegate
In Uinavigationbar, there are also one of the following properties:
@property(nullable,nonatomic,weak) id<UINavigationBarDelegate> delegate;
With proxies, we can monitor some push and pop operations on the navigation bar:
//item将要push的时候调用,返回NO,则不能push
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item;
//item已经push后调用
- (void)navigationBar:(UINavigationBar *)navigationBar didPushItem:(UINavigationItem *)item;
//item将要pop时调用,返回NO,不能pop
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item;
//item已经pop后调用
- (void)navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item;
Second, the tool bar Uitoolbar
the navigation bar generally appears in the head of the view, in contrast, the toolbar will generally appear at the bottom of the view, which can be filled with buttons to provide users with some action. Create a toolbar as follows:
self.view.backgroundColor = [UIColor grayColor];
UIToolbar * tool = [[UIToolbar alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-40, 320, 40)];
[self.view addSubview:tool];
Here are some of the methods in Uitoolbar, most of which are covered in Uinavigationbar, and here's a simple introduction:
The style of the toolbar, similar to the navigation bar, has two black and white
@property (nonatomic) Uibarstyle Barstyle;
Set up an array of buttons on a toolbar
@property (nullable,nonatomic,copy) Nsarray<uibarbuttonitem *> *items;
Set whether the toolbar is transparent
@property (nonatomic,assign,getter=istranslucent) BOOL translucent;
To set a toolbar button
-(void) Setitems: (Nullable nsarray<uibarbuttonitem *> *) Items animated: (BOOL) animated;
Set Item style color
@property (null_resettable, Nonatomic,strong) Uicolor *tintcolor;
Set Toolbar background color
@property (Nullable, Nonatomic,strong) Uicolor *bartintcolor;
Set toolbar background and shadow patterns
-(void) SetBackgroundImage: (Nullable uiimage *) backgroundimage fortoolbarposition: (uibarposition) Toporbottom Barmetrics: (uibarmetrics) barmetrics;
-(Nullable UIImage *) Backgroundimagefortoolbarposition: (uibarposition) Toporbottom barmetrics: (uibarmetrics) Barmetrics;
-(void) Setshadowimage: (Nullable uiimage *) ShadowImage fortoolbarposition: (uibarposition) Toporbottom;
-(Nullable UIImage *) Shadowimagefortoolbarposition: (uibarposition) Toporbottom;