1. 程式人生 > >iOS 純程式碼定製 UINavigationController導航欄 左按鈕又按鈕以及中間的標題

iOS 純程式碼定製 UINavigationController導航欄 左按鈕又按鈕以及中間的標題

iOS 純程式碼定製 UINavigationController導航欄 左按鈕右按鈕以及中間的標題

1.用xcode6 建立的專案,要用純程式碼的話,首頁要關閉自帶storyboard 。
2.然後再appdelegate.m 裡面新增程式碼

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


//1、建立視窗

self.window = [[UIWindowalloc]initWithFrame:[[UIScreen

mainScreen]bounds]];

self.window.backgroundColor = [UIColorwhiteColor];

//2.建立一個導航控制器並新增子檢視

UINavigationController *nav = [[UINavigationControlleralloc]initWithRootViewController:[ViewControllernew]];

//    nav.navigationBar.barTintColor = [UIColor redColor];

    [nav.navigationBarsetBackgroundImage:[

UIImageimageNamed:@"1"]forBarMetrics:UIBarMetricsDefault];

//3.設定根檢視

self.window.rootViewController = nav;

//4、顯示視窗

    [self.windowmakeKeyAndVisible];

returnYES;

}
3.在ViewController 裡面去定製導航欄左右按鈕,以及中間標題

//定製標題

//方式1

//    self.title = @"通訊錄";

//方式2

   UILabel *titleLab = [[UILabelalloc]initWithFrame

:CGRectMake(0,20, 80, 44)];

    titleLab.text =@"通訊錄";

    titleLab.textColor = [UIColorwhiteColor];

    titleLab.textAlignment =NSTextAlignmentCenter;

self.navigationItem.titleView = titleLab;

//定製右按鈕

//方式1:用系統自帶的

self.navigationItem.rightBarButtonItem = [[UIBarButtonItemalloc]initWithTitle:@"下一頁"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(Next:)];

//方式2:自己定製

UIButton *but = [UIButtonbuttonWithType:UIButtonTypeCustom];

    but.frame =CGRectMake(0,0, 60, 44);

    [but setTitle:@"下一頁"forState:UIControlStateNormal];

    [but addTarget:selfaction:@selector(Next:)forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem  *barBut = [[UIBarButtonItemalloc]initWithCustomView:but];

self.navigationItem.rightBarButtonItem = barBut;

//定製左按鈕

UIButton *but = [UIButtonbuttonWithType:UIButtonTypeCustom];

    but.frame =CGRectMake(0,0, 60, 44);

    [but setTitle:@"<返回"forState:UIControlStateNormal];

    [but addTarget:selfaction:@selector(popView:)forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem  *barBut = [[UIBarButtonItemalloc]initWithCustomView:but];

self.navigationItem.leftBarButtonItem = barBut;


ps:每天進步一點點,做一個快樂的程式猿