tabBarViewController


NS_ASSUME_NONNULL_BEGIN

@class UIView, UIImage, UINavigationController, UITabBarItem;
@protocol UITabBarControllerDelegate;

NS_CLASS_AVAILABLE_IOS(2_0) @interface UITabBarController : UIViewController <UITabBarDelegate, NSCoding>
//用於將控制器添加到導航控制器(屬性)
@property(nullable, nonatomic,copy) NSArray<__kindof UIViewController *> *viewControllers;
// If the number of view controllers is greater than the number displayable by a tab bar, a "More" navigation controller will automatically be shown.
// The "More" navigation controller will not be returned by -viewControllers, but it may be returned by -selectedViewController.

//將控制器添加到導航控制器的方法 - (void)setViewControllers:(NSArray<__kindof UIViewController *> * __nullable)viewControllers animated:(BOOL)animated; //這可能會返回“更多”導航控制器如果它存在。 @property(nullable, nonatomic, assign) __kindof UIViewController *selectedViewController; // This may return the "More" navigation controller if it exists.
//點擊下標 @property(nonatomic) NSUInteger selectedIndex; //更多的導航控制器 @property(nonatomic, readonly) UINavigationController *moreNavigationController __TVOS_PROHIBITED; // Returns the "More" navigation controller, creating it if it does not already exist.
@property(nullable, nonatomic, copy) NSArray<__kindof UIViewController *> *customizableViewControllers __TVOS_PROHIBITED; // If non-nil, then the "More" view will include an "Edit" button that displays customization UI for the specified controllers. By default, all view controllers are customizable. //提供-[UIActionSheet showFromTabBar:]。試圖修改標簽欄的內容將直接拋出異常。 @property(nonatomic,readonly) UITabBar *tabBar NS_AVAILABLE_IOS(3_0); // Provided for -[UIActionSheet showFromTabBar:]. Attempting to modify the contents of the tab bar directly will throw an exception. @property(nullable, nonatomic,weak) id<UITabBarControllerDelegate> delegate; @end @protocol UITabBarControllerDelegate <NSObject> @optional
//再某種條件下跳轉到控制器
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController NS_AVAILABLE_IOS(3_0);
//點擊某個tabBar之后,調用的方法(常常用於更改一鍵多功能的tabBar)
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController; - (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED; - (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED; - (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed __TVOS_PROHIBITED; - (UIInterfaceOrientationMask)tabBarControllerSupportedInterfaceOrientations:(UITabBarController *)tabBarController NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED; - (UIInterfaceOrientation)tabBarControllerPreferredInterfaceOrientationForPresentation:(UITabBarController *)tabBarController NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED; - (nullable id <UIViewControllerInteractiveTransitioning>)tabBarController:(UITabBarController *)tabBarController interactionControllerForAnimationController: (id <UIViewControllerAnimatedTransitioning>)animationController NS_AVAILABLE_IOS(7_0); - (nullable id <UIViewControllerAnimatedTransitioning>)tabBarController:(UITabBarController *)tabBarController animationControllerForTransitionFromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC NS_AVAILABLE_IOS(7_0); @end @interface UIViewController (UITabBarControllerItem) @property(null_resettable, nonatomic, strong) UITabBarItem *tabBarItem; // Automatically created lazily with the view controller's title if it's not set explicitly. @property(nullable, nonatomic, readonly, strong) UITabBarController *tabBarController; // If the view controller has a tab bar controller as its ancestor, return it. Returns nil otherwise. @end NS_ASSUME_NONNULL_END

 UItabBar

#import <Foundation/Foundation.h>
#import <UIKit/UIKitDefines.h>
#import <UIKit/UIControl.h>
NS_ASSUME_NONNULL_BEGIN
typedef NS_ENUM(NSInteger, UITabBarItemPositioning) {
//指定自動標簽欄項目定位根據習語的用戶界面,如下:為iPhone用戶界面成語,標簽欄項目填補水平標簽欄,根據需要調整項間的間距iPad的用戶界面,標簽欄項目定位與默認的寬度和密切彼此相鄰項間間距(可定制itemWidth itemSpacing屬性),可能留下空間標簽欄的左和右邊緣
    UITabBarItemPositioningAutomatic,
//默認的標簽欄項目定位在iPhone上。指定標簽欄項目應該分布填補標簽欄的寬度。默認的標簽欄項目定位在iPhone上。指定標簽欄項目應該分布填補標簽欄的寬度。
    UITabBarItemPositioningFill,
//默認的標簽欄項目定位在iPad上。指定標簽欄項目應該定位與默認的寬度和密切彼此相鄰項間間距(可定制的itemWidth和itemSpacing屬性)。標簽欄的項目集中在標簽欄,可能留下空間標簽欄的左和右邊緣。默認的標簽欄項目定位在iPad上。指定標簽欄項目應該定位與默認的寬度和密切彼此相鄰項間間距(可定制的itemWidth和itemSpacing屬性)。標簽欄的項目集中在標簽欄,可能留下空間標簽欄的左和右邊緣。
    UITabBarItemPositioningCentered,
} NS_ENUM_AVAILABLE_IOS(7_0);
@class UITabBarItem;
@class UIImageView;
@protocol UITabBarDelegate;
NS_CLASS_AVAILABLE_IOS(2_0) @interface UITabBar : UIView
//弱引用。默認為零(意味着默認是不執行代理方法的)(0)
@property(nullable,nonatomic,assign) id<UITabBarDelegate> delegate;     // weak reference. default is nil
//獲取/設置uitabbaritem可見。默認是零(0)
@property(nullable,nonatomic,copy) NSArray<UITabBarItem *> *items;        // get/set visible UITabBarItems. default is nil. changes not animated. shown in order
//基於模式將顯示反饋。(點擊之后)
@property(nullable,nonatomic,assign) UITabBarItem *selectedItem; // will show feedback based on mode. default is nil

 //將淡入或淡出或者重新排序和調整間距(動畫效果)
- (void)setItems:(nullable NSArray<UITabBarItem *> *)items animated:(BOOL)animated;   // will fade in or out or reorder and adjust spacing
// Reorder items. This will display a sheet with all the items listed, allow the user to change/reorder items and shows a 'Done' button at the top
- (void)beginCustomizingItems:(NSArray<UITabBarItem *> *)items __TVOS_PROHIBITED;   // list all items that can be reordered. always animates a sheet up. visible items not listed are fixed in place
- (BOOL)endCustomizingAnimated:(BOOL)animated __TVOS_PROHIBITED;    // hide customization sheet. normally you should let the user do it. check list of items to see new layout. returns YES if layout changed
- (BOOL)isCustomizing __TVOS_PROHIBITED;
 /*
 The behavior of tintColor for bars has changed on iOS 7.0. It no longer affects the bar's background
 and behaves as described for the tintColor property added to UIView.
 To tint the bar's background, please use -barTintColor.
 */
@property(null_resettable, nonatomic,strong) UIColor *tintColor NS_AVAILABLE_IOS(5_0);
@property(nullable, nonatomic,strong) UIColor *barTintColor NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;  // default is nil
/* selectedImageTintColor will be applied to the gradient image used when creating the
 selected image. Default is nil and will result in the system bright blue for selected
 tab item images. If you wish to also customize the unselected image appearance, you must
 use the image and selectedImage properties on UITabBarItem along with UIImageRenderingModeAlways
 Deprecated in iOS 8.0. On iOS 7.0 and later the selected image takes its color from the
 inherited tintColor of the UITabBar, which may be set separately if necessary.
 */
@property(nullable,nonatomic,strong) UIColor *selectedImageTintColor NS_DEPRECATED_IOS(5_0,8_0,"Use tintColor") UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED;
/* The background image will be tiled to fit, even if it was not created via the UIImage resizableImage methods.
 */
@property(nullable, nonatomic,strong) UIImage *backgroundImage NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
/* The selection indicator image is drawn on top of the tab bar, behind the bar item icon.
 */
@property(nullable, nonatomic,strong) UIImage *selectionIndicatorImage NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; 
/* 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: (if the default background image is used, the default shadow image will be used).
 */
@property(nullable, nonatomic,strong) UIImage *shadowImage NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;
/*
 Default is UITabBarItemPositioningAutomatic. The tab bar items fill horizontally
 for the iPhone user interface idiom, and are centered with a default width and 
 inter-item spacing (customizable with the itemWidth and itemSpacing
 properties) for the iPad idiom. When the tab bar is owned by a UITabBarController
 further heuristics may determine the positioning for UITabBarItemPositioningAutomatic.
    Use UITabBarItemPositioningFill to force the items to fill horizontally.
    Use UITabBarItemPositioningCenter to force the items to center with a default
 width (or the itemWidth, if set).
 */
@property(nonatomic) UITabBarItemPositioning itemPositioning NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED;

/*
 Set the itemWidth to a positive value to be used as the width for tab bar items
 when they are positioned as a centered group (as opposed to filling the tab bar).
 Default of 0 or values less than 0 will be interpreted as a system-defined width.
 */
@property(nonatomic) CGFloat itemWidth NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;
/*
 Set the itemSpacing to a positive value to be used between tab bar items
 when they are positioned as a centered group.
 Default of 0 or values less than 0 will be interpreted as a system-defined spacing.
 */
@property(nonatomic) CGFloat itemSpacing NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;
/*
 Valid bar styles are UIBarStyleDefault (default) and UIBarStyleBlack.
 */
@property(nonatomic) UIBarStyle barStyle NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED;
/*
 Default is YES.
 You may force an opaque background by setting the property to NO.
 If the tab bar has a custom background image, the default is inferred from the alpha
 values of the image—YES if it has any pixel with alpha < 1.0
 If you send setTranslucent:YES to a tab bar with an opaque custom background imag
 the tab bar will apply a system opacity less than 1.0 to the image.
 If you send setTranslucent:NO to a tab bar with a translucent custom background image
 the tab bar will provide an opaque background for the image using the bar's barTintColor if defined, or black
 for UIBarStyleBlack or white for UIBarStyleDefault if barTintColor is nil.
 */
@property(nonatomic,getter=isTranslucent) BOOL translucent NS_AVAILABLE_IOS(7_0);
@end
//___________________________________________________________________________________________________
@protocol UITabBarDelegate<NSObject>
@optional
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item; // called when a new view is selected by the user (but not programatically)
/* called when user shows or dismisses customize sheet. you can use the 'willEnd' to set up what appears underneath. 
 changed is YES if there was some change to which items are visible or which order they appear. If selectedItem is no longer visible, 
 it will be set to nil.
 */
- (void)tabBar:(UITabBar *)tabBar willBeginCustomizingItems:(NSArray<UITabBarItem *> *)items __TVOS_PROHIBITED;                     // called before customize sheet is shown. items is current item list
- (void)tabBar:(UITabBar *)tabBar didBeginCustomizingItems:(NSArray<UITabBarItem *> *)items __TVOS_PROHIBITED;                      // called after customize sheet is shown. items is current item list
- (void)tabBar:(UITabBar *)tabBar willEndCustomizingItems:(NSArray<UITabBarItem *> *)items changed:(BOOL)changed __TVOS_PROHIBITED; // called before customize sheet is hidden. items is new item list
- (void)tabBar:(UITabBar *)tabBar didEndCustomizingItems:(NSArray<UITabBarItem *> *)items changed:(BOOL)changed __TVOS_PROHIBITED;  // called after customize sheet is hidden. items is new item list
@end
NS_ASSUME_NONNULL_END

UItabBarItem

#import <Foundation/Foundation.h>
#import <UIKit/UIBarItem.h>
#import <UIKit/UIGeometry.h>
#import <UIKit/UIKitDefines.h>

NS_ASSUME_NONNULL_BEGIN

typedef NS_ENUM(NSInteger, UITabBarSystemItem) {
    UITabBarSystemItemMore,
    UITabBarSystemItemFavorites,
    UITabBarSystemItemFeatured,
    UITabBarSystemItemTopRated,
    UITabBarSystemItemRecents,
    UITabBarSystemItemContacts,
    UITabBarSystemItemHistory,
    UITabBarSystemItemBookmarks,
    UITabBarSystemItemSearch,
    UITabBarSystemItemDownloads,
    UITabBarSystemItemMostRecent,
    UITabBarSystemItemMostViewed,
};

@class UIView, UIImage;

NS_CLASS_AVAILABLE_IOS(2_0) @interface UITabBarItem : UIBarItem 

- (instancetype)init NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
/* The unselected image is autogenerated from the image argument. The selected image
 is autogenerated from the selectedImage if provided and the image argument otherwise.
 To prevent system coloring, provide images with UIImageRenderingModeAlwaysOriginal (see UIImage.h)
 */
//直接定義工具條標題,標題圖片,及tag值 - (instancetype)initWithTitle:(nullable NSString *)title image:(nullable UIImage *)image tag:(NSInteger)tag;
//定義工具條標題,標題圖片,選中之后的標題圖片
- (instancetype)initWithTitle:(nullable NSString *)title image:(nullable UIImage *)image selectedImage:(nullable UIImage *)selectedImage NS_AVAILABLE_IOS(7_0); - (instancetype)initWithTabBarSystemItem:(UITabBarSystemItem)systemItem tag:(NSInteger)tag; //選中之后的拖欠 @property(nullable, nonatomic,strong) UIImage *selectedImage NS_AVAILABLE_IOS(7_0); //這些方法現在已經棄用 @property(nullable, nonatomic, copy) NSString *badgeValue; // default is nil /* These methods are now deprecated. Please use -initWithTitle:image:selectedImage:. */ - (void)setFinishedSelectedImage:(nullable UIImage *)selectedImage withFinishedUnselectedImage:(nullable UIImage *)unselectedImage NS_DEPRECATED_IOS(5_0,7_0,"Use initWithTitle:image:selectedImage: or the image and selectedImage properties along with UIImageRenderingModeAlwaysOriginal") __TVOS_PROHIBITED; - (nullable UIImage *)finishedSelectedImage NS_DEPRECATED_IOS(5_0,7_0) __TVOS_PROHIBITED; - (nullable UIImage *)finishedUnselectedImage NS_DEPRECATED_IOS(5_0,7_0) __TVOS_PROHIBITED; /* To set item label text attributes use the appearance selectors available on the superclass, UIBarItem. Use the following to tweak the relative position of the label within the tab button (for handling visual centering corrections if needed because of custom text attributes) */ @property (nonatomic, readwrite, assign) UIOffset titlePositionAdjustment NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; @end NS_ASSUME_NONNULL_END

UIBarItem

#import <Foundation/Foundation.h>
#import <UIKit/UIGeometry.h>
#import <UIKit/UIKitDefines.h>
#import <UIKit/UIAppearance.h>
#import <UIKit/UIControl.h>

NS_ASSUME_NONNULL_BEGIN

@class UIImage;

NS_CLASS_AVAILABLE_IOS(2_0) @interface UIBarItem : NSObject <NSCoding, UIAppearance>

- (instancetype)init NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;

@property(nonatomic,getter=isEnabled) BOOL         enabled;      // default is YES(是否可點)
@property(nullable, nonatomic,copy)             NSString    *title;        // default is nil(工具條主題名字)
@property(nullable, nonatomic,strong)           UIImage     *image;        // default is nil(工具條主題圖標)
@property(nullable, nonatomic,strong)           UIImage     *landscapeImagePhone NS_AVAILABLE_IOS(5_0) __TVOS_PROHIBITED; // default is nil
@property(nonatomic)                  UIEdgeInsets imageInsets;  // default is UIEdgeInsetsZero
@property(nonatomic)                  UIEdgeInsets landscapeImagePhoneInsets NS_AVAILABLE_IOS(5_0) __TVOS_PROHIBITED;  // default is UIEdgeInsetsZero. These insets apply only when the landscapeImagePhone property is set.
@property(nonatomic)                  NSInteger    tag;          // default is 0

/* 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.
 */
- (void)setTitleTextAttributes:(nullable NSDictionary<NSString *,id> *)attributes forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
- (nullable NSDictionary<NSString *,id> *)titleTextAttributesForState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

@end

NS_ASSUME_NONNULL_END

定義tabBarController控制器:(UITabBarButton在UITabBar中得位置是均分的,UITabBar的高度為49。)

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBar.backgroundColor = [UIColor grayColor]; //床架控制器 self.V1 = [[ViewController alloc] init]; //將控制器放入導航控制器中 UINavigationController *navc= [[UINavigationController alloc] initWithRootViewController:self.V1]; //設置tabBarItem的標題 navc.tabBarItem.title = @"成長"; //設置tabBarItem的標題圖片 navc.tabBarItem.image = [UIImage imageNamed:[NSString stringWithFormat:@"tab_growth"]]; // navc.tabBarItem.image = [self YZImageWithColor:[UIColor whiteColor] andSize:CGSizeMake(CGRectGetWidth(self.tabBar.bounds)/4, CGRectGetHeight(self.tabBar.bounds))]; //設置tabBarItem標題點擊之前的顏色 [navc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:10]} forState:UIControlStateNormal]; //設置tabBarItem標題點擊之后的顏色 [navc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor redColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:10]} forState:UIControlStateSelected]; self.V3 = [[TwoViewController alloc] init]; UINavigationController *navc1= [[UINavigationController alloc] initWithRootViewController:self.V3]; navc1.navigationBar.barTintColor = [UIColor blueColor]; navc1.tabBarItem.title = @"我"; navc1.tabBarItem.image = [UIImage imageNamed:[NSString stringWithFormat:@"tab_mine"]]; // navc1.tabBarItem.image = [self YZImageWithColor:[UIColor whiteColor] andSize:CGSizeMake(CGRectGetWidth(self.tabBar.bounds)/4, CGRectGetHeight(self.tabBar.bounds))];  [navc1.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:10]} forState:UIControlStateNormal]; [navc1.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor redColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:10]} forState:UIControlStateSelected]; //將控制器放入數組中 NSArray * array = [NSArray arrayWithObjects:navc, navc1, nil]; //將控制器  [self setViewControllers:array animated:YES]; //調用函數設置tabBar背景顏色 self.tabBar.barTintColor = [UIColor purpleColor]; //調用函數設置tabBar對應的每一個控制器點擊之后的圖標 self.tabBar.selectionIndicatorImage = [self YZImageWithColor:[UIColor orangeColor] andSize:CGSizeMake(CGRectGetWidth(self.tabBar.bounds)/4, CGRectGetHeight(self.tabBar.bounds))]; //重寫UITabBarController代理 self.delegate = self; }

//設置圖片的顏色和大小

 
         

- (UIImage *)YZImageWithColor:(UIColor *)color andSize:(CGSize)size{

    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);

    UIGraphicsBeginImageContext(rect.size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [color CGColor]);

    CGContextFillRect(context, rect);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return image;

}

上方代碼我們能夠得到想要的任何形式的固定的tabBarController但是呢我們有時候需求需要給同一個tabBar控制器的主題添加不同的功能,這時候使用普通的tabBarController已經不能夠滿足我們的需求了。這時候我們就用到了他的代理方法

//點擊tabBarController之后調用的時間
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{

    UITabBarItem *tabBarItem = [[self.viewControllers objectAtIndex:0] tabBarItem]; if(tabBarController.selectedIndex == 0){ if ([tabBarItem.title isEqualToString:@"相機"]) { //調用相機相冊  [self yzxShowCamera]; } tabBarItem.image = [UIImage imageNamed:[NSString stringWithFormat:@"tab_ camera"]]; tabBarItem.title = @"相機"; }else{ tabBarItem.title = @"成長"; tabBarItem.image = [UIImage imageNamed:[NSString stringWithFormat:@"tab_growth"]]; } }

得到能夠改變的tabBar

我們使用以上方法是的我們能夠定義我們想要的tabBarController了,當然我們亦可以自定義tabBarController但是這個往往會使得我們的界面更加復雜話。而且調用原聲的往往更加簡單.

下方的工具條稱為UITabBar ,如果UITabBarController有N個子控制器,那么UITabBar內部就會有N 個UITabBarButton作為子控件與之對應。

下面的tabBar按鈕顯示是有tabBarItem決定的:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM