iOS中的UINavigationController(導航控制器)

先在AppDelegate類裏面添加窗口和導航控制器數組

#import "AppDelegate.h"
////!!!切記:引入別的視圖的.h頭文件
#import "ViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


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

    //建立Window
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor redColor];
    //讓當前的Window成爲主窗口
    [self.window makeKeyAndVisible];
    
    //設置Window的根視圖
    ViewController *a = [[ViewController alloc]init];
    
    
    //1.建立一個導航控制器
    UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:a];
    //2.設置導航控制器爲window的根視圖
    self.window.rootViewController=nav;

    
    //給導航控制器着色
    nav.navigationBar.barTintColor = [UIColor yellowColor];
    
    //給導航欄添加圖片
    [nav.navigationBar setBackgroundImage:[UIImage imageNamed:@"qidong.jpg"] forBarMetrics:UIBarMetricsDefault];
    
    //設置導航的透明度
    [nav.navigationBar setBarStyle:UIBarStyleBlack];
    nav.navigationBar.translucent = YES;
    
    
    return YES;
}

 

 

接着在第一個視圖裏面設置一個Button,點擊跳到下一個界面,裏面有導航欄的各類設置微信

#import "ViewController.h"
////!!!切記:引入別的視圖的.h頭文件
#import "TwoViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //新建一個按鈕
    UIButton *myButtonOne = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    myButtonOne.backgroundColor = [UIColor blueColor];
    [myButtonOne addTarget:self action:@selector(haha:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:myButtonOne];
    
    //設置當前界面的顏色
    self.view.backgroundColor = [UIColor orangeColor];
    
    //設置導航欄的標題
//    self.navigationItem.title = @"微信";
    
    //自定義標題(也就是導航欄裏面中間的標題能夠點擊,因此是個Button)
    UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //設置標題的大小
    titleButton.frame = CGRectMake(0, 0, 100, 40);
    //設置標題文字的內容
    [titleButton setTitle:@"我能夠點擊" forState:UIControlStateNormal];
    //設置標題文字的顏色
    [titleButton setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
    //設置標題文字的字體和大小
    titleButton.titleLabel.font = [UIFont systemFontOfSize:20];
    //給標題文字(實際上是個Button)添加觸動方法
    [titleButton addTarget:self action:@selector(xixi:) forControlEvents:UIControlEventTouchUpInside];
    //設置標題文字的底色
    [titleButton setBackgroundColor:[UIColor redColor]];
    //將標題文字(實際上是個Button)賦給導航欄的標題
    self.navigationItem.titleView =titleButton;
    
    
    //設置左右兩邊的按鈕
    
    //設置左邊的按鈕
//    UIBarButtonItem *left = [[UIBarButtonItem alloc]initWithTitle:@"左邊" style:UIBarButtonItemStylePlain target:self action:@selector(selfhaha:)];
//    self.navigationItem.leftBarButtonItem = left;
    
    //設置右邊的按鈕
    UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithTitle:@"右邊" style:UIBarButtonItemStylePlain target:self action:@selector(righthaha:)];
    
    //設置右邊第二個按鈕
    UIBarButtonItem *rightTwo = [[UIBarButtonItem alloc]initWithTitle:@"右二" style:UIBarButtonItemStylePlain target:self action:@selector(rightTwohaha:)];
    
    //設置右邊第三個按鈕
    UIBarButtonItem *rightThree = [[UIBarButtonItem alloc]initWithTitle:@"右三" style:UIBarButtonItemStylePlain target:self action:@selector(rightThreehaha:)];
    //若是一次有不少按鈕的話,要用數組來設置
    self.navigationItem.rightBarButtonItems = @[right,rightTwo,rightThree];
    
    
    //將左邊的按鈕換成圖片
    UIBarButtonItem *leftImage = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"666"] style:UIBarButtonItemStylePlain target:self action:@selector(leftImagehaha:)];
    self.navigationItem.leftBarButtonItem = leftImage;
    
    
    
    
    
}
//按鈕的方法
-(void)haha:(id)a{
    
    //push到下一個界面
    TwoViewController *x = [[TwoViewController alloc]init];
    [self.navigationController pushViewController:x animated:YES];
    
    
}
-(void)xixi:(id)a{
    NSLog(@"我是中間的標題,我被點擊了");
}
-(void)selfhaha:(id)a{
    NSLog(@"我是左邊");
}
-(void)righthaha:(id)a{
    NSLog(@"我是右邊");
}
-(void)rightTwohaha:(id)a{
    NSLog(@"我是右二");
}
-(void)rightThreehaha:(id)a{
    NSLog(@"我是右三");
}
-(void)leftImagehaha:(id)a{
    NSLog(@"我是左邊的圖片");
}

 

 

接着在第二個視圖裏面設置一個Button,點擊跳到下一個界面app

#import "TwoViewController.h"
//!!!切記:引入別的視圖的.h頭文件
#import "ThreeViewController.h"

@interface TwoViewController ()

@end

@implementation TwoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //設置界面顏色
    self.view.backgroundColor = [UIColor purpleColor];
    
    //新建一個按鈕
    UIButton *myButtonOne = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    myButtonOne.backgroundColor = [UIColor blueColor];
    [myButtonOne addTarget:self action:@selector(haha2:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:myButtonOne];
    
}
//按鈕的方法
-(void)haha2:(id)a{
    //跳到指定的頁面
    ThreeViewController *x = [[ThreeViewController alloc]init];
    [self.navigationController pushViewController:x animated:YES];
    
}

 

 

接着在第三個視圖裏面設置一個Button,點擊跳回上一個頁面、或者任意視圖,或者直接返回主視圖ide

#import "ThreeViewController.h"
//!!!切記:引入別的視圖的.h頭文件
#import "ViewController.h"

@interface ThreeViewController ()

@end

@implementation ThreeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //設置界面的顏色
    self.view.backgroundColor = [UIColor greenColor];
    
    //新建一個按鈕
    UIButton *myButtonOne = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    myButtonOne.backgroundColor = [UIColor blueColor];
    [myButtonOne addTarget:self action:@selector(haha3:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:myButtonOne];
    
}
//按鈕的方法
-(void)haha3:(id)a{
    //!!!切記:引入別的視圖的.h頭文件
    //跳到最根的視圖
    [self.navigationController popToRootViewControllerAnimated:YES];
    
    //返回上一級視圖
    [self.navigationController popViewControllerAnimated:YES];
    
    //跳到任意指定視圖
    for (UIViewController *temp in self.navigationController.viewControllers) {
        
        if ([temp isKindOfClass:[ViewController class]]) {
            [self.navigationController popToViewController:temp animated:YES];
        }
    }
    
    
}

 

 

直接設置導航欄上的標題字體和顏色字體

//直接設置導航欄上的標題字體和顏色
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],
          NSForegroundColorAttributeName:[UIColor blackColor]}];

 

 

在當前頁面隱藏導航欄,並設置透明度(從導航欄下面做爲 Y軸 的0起點)spa

//每次進來都會加載一次
- (void)viewWillAppear:(BOOL)animated{
    
    //取消隱藏導航欄(若是要隱藏,改成yes就好了)
    self.navigationController.navigationBarHidden = NO;
    
    //設置導航欄透明度
    self.navigationController.navigationBar.translucent = NO;
 
}

 

 

去掉push到的視圖中 自帶的返回箭頭< 右邊的文字,並改變箭頭 < 的顏色code

//去掉子視圖(push到的視圖)中的返回箭頭 < 右邊的文字
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    
    self.navigationItem.backBarButtonItem = item;
    
    
    //改變上面箭頭的顏色(會所有都改變)
    [[UINavigationBar appearance] setTintColor:[UIColor blackColor]];


    //左邊返回箭頭顏色(改變左邊返回箭頭顏色)
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

第二種方法:orm

//去掉PUSH後的back文字(第二種方法)
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
                                                         forBarMetrics:UIBarMetricsDefault];

 

清除滑動時 cell 中間的縫隙圖片

//清除滑動時中間的縫隙
    choiceTableView.separatorColor=[UIColor clearColor];
相關文章
相關標籤/搜索