iOS開發-微部落格戶端-基本介面搭建(01)

來源:互聯網
上載者:User

1>建立程式載入介面

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

{

    //1>建立視窗

    self.window = [[UIWindowalloc] initWithFrame:[UIScreenmainScreen].bounds];

    //2>設定視窗的根控制器

    UITabBarController *tabBarController = [[UITabBarControlleralloc] init];

    self.window.rootViewController = tabBarController;

    //3>顯示視窗

    [self.windowmakeKeyAndVisible];

    returnYES;

}

 

2>LaunchImage配置

  LaunchImage.launchimage檔案下的Contents.json檔案中記錄了LaunchImage的詳細配置:

  

 3>取消APP表徵圖渲染

  

 4>程式載入時隱藏狀態列

  

  在程式載入完成後如需恢複狀態列顯示,可以在didFinishLaunchingWithOptions方法中調用[application setStatusBarHidden:NO]方法;

 

5>添加TabBar控制器及其子控制器

  自訂一個TabBarViewController類繼承UITabBarController類用來建立自訂的TabBarView,並在該類中的viewDidLoad方法中建立子控制器

- (void)viewDidLoad

{

    [superviewDidLoad];

    //添加子控制器

    UIViewController *home = [[UIViewControlleralloc] init];

    home.view.backgroundColor = [UIColorredColor];

    home.tabBarItem.title = @"首頁";

    home.tabBarItem.image = [UIImageimageNamed:@"tabbar_home"];

    [home.tabBarItemsetSelectedImage:[UIImageimageNamed:@"tabbar_home_selected"]];

    [selfaddChildViewController:home];

    UIViewController *message = [[UIViewControlleralloc] init];

    message.view.backgroundColor = [UIColororangeColor];

    message.tabBarItem.title = @"訊息";

    message.tabBarItem.image = [UIImageimageNamed:@"tabbar_message_center"];

    [message.tabBarItemsetSelectedImage:[UIImageimageNamed:@"tabbar_message_center_selected"]];

    [selfaddChildViewController:message];

    UIViewController *discover = [[UIViewControlleralloc] init];

    discover.view.backgroundColor = [UIColorgreenColor];

    discover.tabBarItem.title = @"發現";

    discover.tabBarItem.image = [UIImage imageNamed:@"tabbar_discover"];

    [discover.tabBarItemsetSelectedImage:[UIImageimageNamed:@"tabbar_discover_selected"]];

    [selfaddChildViewController:discover];

    UIViewController *profile = [[UIViewControlleralloc] init];

    profile.view.backgroundColor = [UIColorblueColor];

    profile.tabBarItem.title = @"我";

    profile.tabBarItem.image = [UIImageimageNamed:@"tabbar_profile"];

    [profile.tabBarItemsetSelectedImage:[UIImageimageNamed:@"tabbar_profile_selected"]];

    [selfaddChildViewController:profile];

}

 6>渲染圖片

    在iOS7中,會對selectedImage的圖片再次渲染為藍色,要想顯示原圖,就必須要取消渲染;

    取消渲染調用的方法:

    selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

 

7>最佳化添加子控制器代碼

    將添加子控制器到TabBarViewController的代碼進行最佳化,建立如下方法:

- (void)addOneChildViewController:(UIViewController *)viewController withTitle:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName

{

    viewController.view.backgroundColor = ZFRandomColor;

    viewController.tabBarItem.title = title;

    viewController.tabBarItem.image = [UIImage imageNamed:imageName];

UIImage *image = [UIImage imageNamed:selectedImageName];

if (iOS7) {

        image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    }

    [viewController.tabBarItem setSelectedImage:image];

    [self addChildViewController:viewController];

}

    其中ZFRandomColor和iOS7為自訂宏,其宏定義在Prefix.pch檔案下:

#ifdef __OBJC__

    #import <UIKit/UIKit.h>

    #import <Foundation/Foundation.h>

    #import <CoreData/CoreData.h>

#define ZFRandomColor [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]

#define iOS7 [[UIDevice currentDevice].systemVersion doubleValue] >= 7.0

#endif

    由於imageWithRenderingMode方法只在iOS7環境下有效,因此此處代碼需要添加條件判斷語句進行系統適配,通過擷取當前運行環境的系統版本來判斷是否編譯此方法;

8>圖片適配

    為UIImage添加一個分類,用於image的系統適配:

@implementation UIImage (Extension)

+ (UIImage *)imageWithName:(NSString *)imageName

{

UIImage *image = nil;

if (iOS7) {

NSString *name = [imageName stringByAppendingString:@"_os7"];

        image = [UIImage imageNamed:name];

    }

if (!image) {

        image = [UIImage imageNamed:imageName];

    }

return image;

}

@end

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.