ios 用純程式碼寫程式的時候,navigationController的導覽列的設定

來源:互聯網
上載者:User

標籤:gen   make   tom   main   origin   tabbar   string   item   turn   

  我們都知道,如果用storyBoard設定導覽列很容易,點擊左右item的時候,進入下一個介面,導覽列的顏色是跟上一層的是一樣的,用純程式碼寫的時候,可以在當前控制器,和從當前控制器進入到下一個控制器都用代碼實現對導覽列的控制,但是,每次都寫代碼設定,很麻煩,所以,可以這樣:

建立一個MainTabBarController的類,在Appdelegate.m裡面完成:

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

    // Override point for customization after application launch.

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

    self.window.rootViewController = [[MainTabBarController alloc] init];

    [self.window makeKeyAndVisible];

    return YES;

}

然後,在MainTabBarController.m裡面

- (void)viewDidLoad {

    [super viewDidLoad];

    [self setUpAllChildVc];

}

 

- (void)setUpAllChildVc

{

//有幾個介面,設定幾個

    MapViewController *HomeVC = [[HFMapViewController alloc] init];

    [self setUpOneChildVcWithVc:HomeVC Image:@"地圖-首頁" selectedImage:@"地圖選中-首頁" title:@"地圖"];

 

    AppointmentViewController *appointmentVC = [[HFAppointmentViewController alloc] init];

    [self setUpOneChildVcWithVc:appointmentVC Image:@"預約-首頁" selectedImage:@"預約選中-首頁" title:@"預約"];

 }

 

- (void)setUpOneChildVcWithVc:(UIViewController *)Vc Image:(NSString *)image selectedImage:(NSString *)selectedImage title:(NSString *)title

{

    MainNavigationController *nav = [[MainNavigationController alloc] initWithRootViewController:Vc];

 

    UIImage *myImage = [UIImage imageNamed:image];

    myImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

 

    //tabBarItem,是系統提供模型,專門負責tabbar上按鈕的文字以及圖片展示

    Vc.tabBarItem.image = myImage;

 

    UIImage *mySelectedImage = [UIImage imageNamed:selectedImage];

    mySelectedImage = [mySelectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

 

    Vc.tabBarItem.selectedImage = mySelectedImage;

 

    Vc.tabBarItem.title = title;

 

    [self addChildViewController:nav];

    

}

只要需要導覽列的,都可以把當前控制器設定為MainNavigationController的跟試圖控制器

ios 用純程式碼寫程式的時候,navigationController的導覽列的設定

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.