iOS開發UITabbarController常用屬性方法


//

//  AppDelegate.m

//  UITabBarController+UINavigationController

#import "AppDelegate.h"

 

@interface AppDelegate ()

 

@end

 

@implementation AppDelegate

 

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    

    //1.創建Window

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    self.window.backgroundColor = [UIColor whiteColor];

    

    //a.初始化一個tabBar控制器

    UITabBarController *tb=[[UITabBarController alloc]init];

    //設置控制器為Window的根控制器

    self.window.rootViewController=tb;

    

    /*

     UITabBarSystemItem的多種設置:

     UITabBarSystemItemMore,//更多圖標

     UITabBarSystemItemFavorites,//最愛圖標

     UITabBarSystemItemFeatured,//特征圖標

     UITabBarSystemItemTopRated,//高級圖標

     UITabBarSystemItemRecents,//最近圖標

     UITabBarSystemItemContacts,//聯系人圖標

     UITabBarSystemItemHistory,//歷史圖標

     UITabBarSystemItemBookmarks,//圖書圖標

     UITabBarSystemItemSearch,//查找圖標

     UITabBarSystemItemDownloads,//下載圖標

     UITabBarSystemItemMostRecent,//記錄圖標

     UITabBarSystemItemMostViewed,//全部查看圖標

     */

    

    //b.創建子控制器

    UIViewController *c1=[[UIViewController alloc]init];

    c1.view.backgroundColor=[UIColor grayColor];

    c1.view.backgroundColor=[UIColor greenColor];

    c1.tabBarItem.title=@"消息";//標題文字

    //設置標題的位置偏移

    //@property (nonatomic, readwrite, assign) UIOffset titlePositionAdjustment;

    c1.tabBarItem.image=[UIImage imageNamed:@"1"];//圖標

    //landscapeImagePhone 橫屏是的圖標

    //imageInsets  圖標位置的偏移

    //landscapeImagePhoneInsets  橫屏時圖標的偏移

    

    /*

    設置和獲取標題的字體屬性

    - (void)setTitleTextAttributes:(nullable NSDictionary<NSString *,id> *)attributes forState:(UIControlState)state;

    - (nullable NSDictionary<NSString *,id> *)titleTextAttributesForState:(UIControlState)state;

     */

    c1.tabBarItem.selectedImage = [UIImage imageNamed:@"11"];//選中時的圖標

    c1.tabBarItem.badgeValue=@"123";//提醒數字

    

    UIViewController *c2=[[UIViewController alloc]init];

    c2.view.backgroundColor=[UIColor brownColor];

    c2.tabBarItem.title=@"聯系人";

    c2.tabBarItem.image=[UIImage imageNamed:@"2"];

    

    UIViewController *c3=[[UIViewController alloc]init];

    c3.view.backgroundColor = [UIColor orangeColor];

    c3.tabBarItem.title=@"動態";

    c3.tabBarItem.image=[UIImage imageNamed:@"3"];

    

    UIViewController *c4=[[UIViewController alloc]init];

    c4.view.backgroundColor = [UIColor grayColor];

    c4.tabBarItem.title=@"設置";

    c4.tabBarItem.image=[UIImage imageNamed:@"4"];

    

    

    //c.添加子控制器到ITabBarController中

    //c.1第一種方式

    //    [tb addChildViewController:c1];

    //    [tb addChildViewController:c2];

    

    //c.2第二種方式

    tb.viewControllers=@[c1,c2,c3,c4];

    

    

    //2.設置Window為主窗口並顯示出來

    [self.window makeKeyAndVisible];

    

    

    

    

    // Override point for customization after application launch.

    return YES;

}

#pragma mark UITabBar屬性和方法

/*

 設置標簽

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

 //設置選中的標簽

 @property(nullable,nonatomic,assign) UITabBarItem *selectedItem;

 - (void)setItems:(nullable NSArray<UITabBarItem *> *)items animated:(BOOL)animated;

 

 

 

 設置自定義標簽

 //調用這個方法會彈出一個類似上面第二張截圖的控制器,我們可以交換標簽的布局順序

 - (void)beginCustomizingItems:(NSArray<UITabBarItem *> *)items;

 //完成標簽布局

 - (BOOL)endCustomizingAnimated:(BOOL)animated;

 //是否正在自定義標簽布局

 - (BOOL)isCustomizing;

 

 

 //設置tabbar顏色

 //設置渲染顏色,會影響選中字體和圖案的渲染

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

 //設置導航欄的顏色

 @property(nullable, nonatomic,strong) UIColor *barTintColor;

 

 

 設置背景圖片

 //設置導航欄背景圖案

 @property(nullable, nonatomic,strong) UIImage *backgroundImage;

 //設置選中一個標簽時,標簽背后的選中提示圖案 這個會出現在設置的item圖案的后面

 @property(nullable, nonatomic,strong) UIImage *selectionIndicatorImage;

 //設置陰影的背景圖案

 @property(nullable, nonatomic,strong) UIImage *shadowImage

 

 

 

 tabbar標簽的屬性

 //設置標簽item的位置模式

 @property(nonatomic) UITabBarItemPositioning itemPositioning;

 //枚舉如下

 typedef NS_ENUM(NSInteger, UITabBarItemPositioning) {

 UITabBarItemPositioningAutomatic,//自動

 UITabBarItemPositioningFill,//充滿

 UITabBarItemPositioningCentered,//中心

 } NS_ENUM_AVAILABLE_IOS(7_0);

 //設置item寬度

 @property(nonatomic) CGFloat itemWidth;

 //設置item間距

 @property(nonatomic) CGFloat itemSpacing;

 

 

 設置tabbar的風格和透明

 //風格 分黑白兩種

 @property(nonatomic) UIBarStyle barStyle;

 //是否透明效果

 @property(nonatomic,getter=isTranslucent) BOOL translucent;

 

 

 delegate

 //選中標簽時調用

 - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item;

 //將要開始編輯標簽時

 - (void)tabBar:(UITabBar *)tabBar willBeginCustomizingItems:(NSArray<UITabBarItem *> *)items;          //已經開始編輯標簽時

 - (void)tabBar:(UITabBar *)tabBar didBeginCustomizingItems:(NSArray<UITabBarItem *> *)items;

 //將要進入編輯狀態時

 - (void)tabBar:(UITabBar *)tabBar willEndCustomizingItems:(NSArray<UITabBarItem *> *)items changed:(BOOL)changed;

 //已經進入編輯狀態時

 - (void)tabBar:(UITabBar *)tabBar didEndCustomizingItems:(NSArray<UITabBarItem *> *)items changed:(BOOL)changed;

 

 */

 

 

- (void)applicationWillResignActive:(UIApplication *)application {

    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.

}

 

 

- (void)applicationDidEnterBackground:(UIApplication *)application {

    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}

 

 

- (void)applicationWillEnterForeground:(UIApplication *)application {

    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.

}

 

 

- (void)applicationDidBecomeActive:(UIApplication *)application {

    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}

 

 

- (void)applicationWillTerminate:(UIApplication *)application {

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}

 

 

@end

 


免責聲明!

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



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