UINavigationBar 一个有样式的view
Posted 人生路1/5
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UINavigationBar 一个有样式的view相关的知识,希望对你有一定的参考价值。
UINavigationBar 继承自,UIView。你可以把它看为是一个特殊的view。
他也是可以独立使用的。
UINavigationBar *bar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 414, 80)];
[self.view addSubview:bar];
好下面是 UINavigationBar 的一些属性和方法
1 //NS_CLASS_AVAILABLE_ios(2_0) @interface UINavigationBar : UIView <NSCoding, UIBarPositioning>
2 //
3 //UINavigationBar 的样式
4 //typedef NS_ENUM(NSInteger, UIBarStyle) {
5 // UIBarStyleDefault = 0,//默认
6 // UIBarStyleBlack = 1,//黑色
7 //}
8 @property(nonatomic,assign) UIBarStyle barStyle __TVOS_PROHIBITED;
9
10 //代理
11 @property(nullable,nonatomic,weak) id<UINavigationBarDelegate> delegate;
12 //
13 ///*
14 // New behavior on iOS 7.
15 // Default is YES.
16 // You may force an opaque background by setting the property to NO.
17 // If the navigation bar has a custom background image, the default is inferred
18 // from the alpha values of the image—YES if it has any pixel with alpha < 1.0
19 // If you send setTranslucent:YES to a bar with an opaque custom background image
20 // it will apply a system opacity less than 1.0 to the image.
21 // If you send setTranslucent:NO to a bar with a translucent custom background image
22 // it will provide an opaque background for the image using the bar‘s barTintColor if defined, or black
23 // for UIBarStyleBlack or white for UIBarStyleDefault if barTintColor is nil.
24 // */
25 //是否透明
26 @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
27 //
28 //// Pushing a navigation item displays the item‘s title in the center of the navigation bar.
29 //// The previous top navigation item (if it exists) is displayed as a "back" button on the left.
30 //push一个 Item(UINavigationItem)
31 - (void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated;
32 //pop UINavigationItem
33 - (nullable UINavigationItem *)popNavigationItemAnimated:(BOOL)animated; // Returns the item that was popped.
34 //
35 //当前push到最上层的item
36 @property(nullable, nonatomic,readonly,strong) UINavigationItem *topItem;
37 //仅次于最上层的item,一般式被推向导航栏左侧的item
38 @property(nullable, nonatomic,readonly,strong) UINavigationItem *backItem;
39 //
40 //获取堆栈中所有item的数组
41 @property(nullable,nonatomic,copy) NSArray<UINavigationItem *> *items;
42 //设置一组item
43 - (void)setItems:(nullable NSArray<UINavigationItem *> *)items animated:(BOOL)animated; // If animated is YES, then simulate a push or pop depending on whether the new top item was previously in the stack.
44 //
45 ///*
46 // The behavior of tintColor for bars has changed on iOS 7.0. It no longer affects the bar‘s background
47 // and behaves as described for the tintColor property added to UIView.
48 // To tint the bar‘s background, please use -barTintColor.
49 // */
50 //影响到导航栏上左侧pop按钮的图案颜色和字体颜色
51 @property(null_resettable, nonatomic,strong) UIColor *tintColor;
52 //设置导航栏的背景色,这个属性被设置后,半透明的效果将失效
53 @property(nullable, nonatomic,strong) UIColor *barTintColor NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR; // default is nil
54 //
55 ///* In general, you should specify a value for the normal state to be used by other states which don‘t have a custom value set.
56 //
57 // Similarly, when a property is dependent on the bar metrics (on the iPhone in landscape orientation, bars have a different height from standard), be sure to specify a value for UIBarMetricsDefault.
58 // */
59 //
60 - (void)setBackgroundImage:(nullable UIImage *)backgroundImage forBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;
61 - (nullable UIImage *)backgroundImageForBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;
62
63 // Same as using UIBarPositionAny in -setBackgroundImage:forBarPosition:barMetrics. Resizable images will be stretched
64 // vertically if necessary when the navigation bar is in the position UIBarPositionTopAttached.
65 // */
66 - (void)setBackgroundImage:(nullable UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
67 - (nullable UIImage *)backgroundImageForBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
68 //上面两个方法用于设置和获取导航栏的背景图案,这里需要注意,默认背景图案是不做缩放处理的,所以我们使用的图片尺寸要和导航栏尺寸匹配,这里面还有一个UIBarMetrics参数,这个参数设置设备的状态,如下:
69 //typedef NS_ENUM(NSInteger, UIBarMetrics) {
70 // UIBarMetricsDefault,//正常竖屏状态
71 // UIBarMetricsCompact,//横屏状态
72 //};
73 //
74
75 ///* Default is nil. When non-nil, a custom shadow image to show instead of the default shadow image. For a custom shadow to be shown, a custom background image must also be set with -setBackgroundImage:forBarMetrics: (if the default background image is used, the default shadow image will be used).
76 // */
77 //设置导航栏的阴影图片
78 @property(nullable, nonatomic,strong) UIImage *shadowImage NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;
79 //
80 ///* You may specify the font, text color, and shadow properties for the title in the text attributes dictionary, using the keys found in NSAttributedString.h.
81 // */
82 //设置导航栏的标题字体属性
83 //例如:bar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor redColor]};
84 @property(nullable,nonatomic,copy) NSDictionary<NSString *,id> *titleTextAttributes NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
85 //
86 //设置导航栏标题的竖直位置偏移
87 - (void)setTitleVerticalPositionAdjustment:(CGFloat)adjustment forBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
88 - (CGFloat)titleVerticalPositionAdjustmentForBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
89 //
90 ///*
91 // The back indicator image is shown beside the back button.
92 // The back indicator transition mask image is used as a mask for content during push and pop transitions
93 // Note: These properties must both be set if you want to customize the back indicator image.
94 // */
95 //返回按钮的样式(这两个一起设置)
96 @property(nullable,nonatomic,strong) UIImage *backIndicatorImage NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED;
97 @property(nullable,nonatomic,strong) UIImage *backIndicatorTransitionMaskImage NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED;
98 //
99 @end
UINavigationBarDelegate
1 //item将要push的时候调用,返回NO,则不能push
2 - (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item;
3 //item已经push后调用
4 - (void)navigationBar:(UINavigationBar *)navigationBar didPushItem:(UINavigationItem *)item;
5 //item将要pop时调用,返回NO,不能pop
6 - (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item;
7 //item已经pop后调用
8 - (void)navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item;
bar上是item(UINavigationItem)这不是一个按钮那么简单。它里面承载着 UIBarButtonItem
以上是关于UINavigationBar 一个有样式的view的主要内容,如果未能解决你的问题,请参考以下文章
如何为 UINavigationBar 的 styleBar 创建自定义样式?
UIStatusBar样式与UINavigationBar一致
在 UIToolbar 上创建左箭头按钮(如 UINavigationBar 的“后退”样式)