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

iOS7中UIBarButtonItem、UINavigationItem、UINavigationBar、UIToolbar之间的关系

2015-07-25 16:02 387 查看
在iOS7中,有一个很常见的控件,叫做导航控制器(UINavigationController),它继承于视图控制器(UIViewController),同时他又属于视图控制器的一个属性。可以通过一个根视图控制器(UIViewController)进行初始化。除了继承自视图控制器(UIViewController)的方法外,它还拥有如下特有方法:

(instancetype)initWithNavigationBarClass:(Class)navigationBarClass toolbarClass:(Class)toolbarClassNS_AVAILABLE_IOS(5_0);

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

- (UIViewController *)popViewControllerAnimated:(BOOL)animated;

// Returns the popped controller.

- (NSArray *)popToViewController:(UIViewController *)viewController
animated:(BOOL)animated;

// Pops view controllers until the one specified is on top. Returns the popped controllers.

- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated;

// Pops until there's only a single view controller left on the stack. Returns the popped controllers.

介绍完了导航控制器(UINavigationController),我们在对UIBarButtonItem、UINavigationItem、UINavigationBar、UIToolbar一一进行分析,首先讲导航栏(UINavigationBar)和工具栏(UIToolbar):

导航栏(UINavigationBar)和工具栏(UIToolbar)有一个共同点,就是它们在与导航控制器(UINavigationController)的联系中都是作为导航控制器的属性出现。在UI界面中它们分别在屏幕上方的状态栏下方和屏幕底部,且都占有44个点的高度,导航栏(UINavigationBar)和工具栏(UIToolbar)都作为一个独立的类,它们都继承于同一个父类视图(UIView).

先看导航栏(UINavigationBar)这个类,除了具有继承自父类视图(View)的特性外,还提供了很多私有(private)的成员变量和众多属性、方法。这些成员变量和属性为我们开发者提供了非常大的自定义和修改系统默认样式的空间。下面部分常用属性和方法。

@property(nonatomic,assign)UIBarStyle barStyle;

@property(nonatomic,assign)id delegate;

// 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;

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

// Returns the item that was popped.

@property(nonatomic,readonly,retain)UINavigationItem *topItem;

@property(nonatomic,readonly,retain)UINavigationItem *backItem;

@property(nonatomic,copy)NSArray *items;

- (void)setItems:(NSArray *)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.

@property(nonatomic,retain)UIColor *tintColor;

@property(nonatomic,retain)UIColor *barTintColorNS_AVAILABLE_IOS(7_0)UI_APPEARANCE_SELECTOR;

// default is nil

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

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

- (void)setBackgroundImage:(UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetricsNS_AVAILABLE_IOS(5_0)UI_APPEARANCE_SELECTOR;

- (UIImage *)backgroundImageForBarMetrics:(UIBarMetrics)barMetricsNS_AVAILABLE_IOS(5_0)UI_APPEARANCE_SELECTOR;

@property(nonatomic,retain)UIImage *backIndicatorImageNS_AVAILABLE_IOS(7_0)UI_APPEARANCE_SELECTOR;

@property(nonatomic,retain)UIImage *backIndicatorTransitionMaskImageNS_AVAILABLE_IOS(7_0)

然后是工具栏(UIToolbar)类,除了具有继承自父类视图(UIView)的所有属性、成员变量和方法外很多自己特有的方法和属性、成员变量。虽说工具栏(UIToolbar)在实际开发中,用的不是很多,但在iOS7中依然占有不可或缺的地位。下面是部分其部分属性和方法:

@property(nonatomic)
UIBarStyle barStyle;

// default is UIBarStyleDefault (blue)

@property(nonatomic,copy)
NSArray *items;

// get/set visible UIBarButtonItem. default is nil. changes not animated. shown in order

- (void)setItems:(NSArray *)items
animated:(BOOL)animated;

// will fade in or out or reorder and adjust spacing

- (void)setBackgroundImage:(UIImage *)backgroundImage forToolbarPosition:(UIBarPosition)topOrBottom
barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

- (UIImage *)backgroundImageForToolbarPosition:(UIBarPosition)topOrBottom barMetrics:(UIBarMetrics)barMetricsNS_AVAILABLE_IOS(5_0)UI_APPEARANCE_SELECTOR;

@property(nonatomic,assign)id<UIToolbarDelegate>
delegateNS_AVAILABLE_IOS(7_0);

// You may not set the delegate when the toolbar is managed by a UINavigationController.

介绍完了导航栏类(UINavigationBar)和工具栏类(UIToolbar)后,我们接着看导航元素项(UINavigationItem)和UIBarButtonItem。导航元素项(UInavigationItem)和导航控制器(UINavigationController)在这四者的关系中,都是最为视图控制器(UIViewController)的一个属性出现,它与导航控制器(UINavigationController)的区别在于导航控制器(UINavigationController)的对象时唯一的,在一个程序中,多个视图控制器共享一个导航控制器(UINavigationController)的对象,导航栏(UINavigationBar)和工具栏(UIToolbar)作为导航控制器的属性,所以二者的对象在同一个程序中同样也是唯一的,也就是说在其他的视图控制器(UIViewController)中对其属性特征进行修改,也会在不同视图控制器(UIViewController)中产生效果。而每一个视图控制器(UINavigationController)对象都可以拥有一个单独的导航元素项(UiNavigationItem)对象,在本视图控制器(UINavigationController)对象中修改其属性特征不会再其他的视图控制器(UINavigationController)中产生影响。

导航元素项(UINavigationItem)对象可以细分为左、中、右三个区域,左边和右边区域可以放一个或者多个UIBarButtonItem,中间则可以放一个title和一个titleView(二者都设置titleView会覆盖title)。当在相应的视图控制器(UIViewController)中创建好一个导航元素项(UINavigationItem)对象时,它会被添加到改视图控制器(UIViewController)的中,并在导航控制器(UINavigationController)的导航栏(UINavigationBar)中显示。 与导航栏(UINaBar)中显示导航元素项(UINavigationItem)不同,工具栏(UIToolbar)则要简单的多,它只能放一个或者多个UIBarButtonItem。并且UIBarButtonItem可在不同的视图控制器(UINavigationController)中单独添加。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: