UI 导航控制器

来源:互联网 发布:人才外包知乎 编辑:程序博客网 时间:2024/05/29 04:53

导航控制器

//1,导航控制器在Appdelegate.h里的启动方法里写:


//  AppDelegate.m

//  Tank_UI_07

//

//  Created by ibokan on 16/1/13.

//  Copyright © 2016谭其伟. All rights reserved.

//


#import "AppDelegate.h"

#import "ViewController.h"   //导入


@interface AppDelegate ()


@end


@implementation AppDelegate



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


    ViewController *viewController = [[ViewControlleralloc]init];

    

    //有一个viewController才能有导航控制器

//    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];   //重新定义一个windows

    

    

    

    self.window.rootViewController = [[UINavigationControlleralloc]initWithRootViewController:viewController];//导航控制器

    [self.windowmakeKeyAndVisible];   //拿到根视图windows(第二层,第一层是mainScreen)

    

    

    //iOS7.0之后通常在appdelegate设置导航栏的颜色

    

    //设置导航栏bar的颜色

    [[UINavigationBarappearance] setBarTintColor:[UIColorcolorWithRed:34/255.0green:199/255.0blue:255/255.0alpha:1]];

    

    //设置导航栏title字体颜色,大小

    [[UINavigationBarappearance] setTitleTextAttributes:[NSDictionarydictionaryWithObjectsAndKeys:[UIColorwhiteColor], NSForegroundColorAttributeName,[UIFontsystemFontOfSize:18],NSFontAttributeName,nil]];

    

    

    

    [[UINavigationBarappearance]setTintColor:[UIColorwhiteColor]];

    

    

    return YES;

}



也可以在mainStoryboard里设置:



//在ViewController.m里

这里涉及Push推动跳转的方法:

//

//  ViewController.m

//  Tank_UI_07

//

//  Created by ibokan on 16/1/13.

//  Copyright © 2016谭其伟. All rights reserved.

//


#import "ViewController.h"

#import "PushView.h"

#import "AViewController.h"

#import "BViewController.h"

#import "CViewController.h"


@interface ViewController ()<UITextFieldDelegate>

{

    UIView *view;

}



@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    

    self.view.backgroundColor = [UIColorwhiteColor];

    

    /*

     UINavigationController  导航控制器层次结构

     

     继承于UIviewController

     

     导航控制器的作用?

     它采用栈的原理来管理视图控制器

     */

    

    //导航控制器显示的view为当前栈顶视图控制器的view,所以对应的self.title为本view的名字后,导航控制器也会自命名为这个名字

    

    self.title =@"Root"//给它一个标题

    

    PushView *pushView = [[PushViewalloc]initWithFrame:CGRectMake([UIScreenmainScreen].bounds.size.width/2-30, [UIScreenmainScreen].bounds.size.height/2-15,60,30)];

    

    [pushView.pushButtonaddTarget:selfaction:@selector(pushButtonAction:)forControlEvents:UIControlEventTouchUpInside];

    

    [self.viewaddSubview:pushView];

    

    

    

    //默认导航控制器的toolbar是隐藏的

    self.navigationController.toolbarHidden =NO;

    


    

    /*---------------关于导航栏----------------*/

    //设置背景,现在开发过程中基本不会设置背景图片,而是设置背景颜色,而设置背景颜色,一般在AppDelegate.m里写

    

    //导航栏的中间有一个titleView

    

    UIButton *titleButton = [UIButtonbuttonWithType:UIButtonTypeCustom];

    [titleButton setTitle:@"touch me"forState:UIControlStateNormal];

    [titleButton setTitleColor:[UIColorpurpleColor] forState:UIControlStateNormal];

    

    //以下的point位置是不起效果的,无论放在哪里都是会居中

    titleButton.frame = CGRectMake(0, 0, 60, 30);

    

    [titleButton addTarget:selfaction:@selector(titleBtnAction:)forControlEvents:UIControlEventTouchUpInside];

    

    self.navigationItem.titleView = titleButton;

    

    

    

    view = [[UIViewalloc]initWithFrame:CGRectMake([UIScreenmainScreen].bounds.size.width/2-75,50, 150, 300)];

    

    view.backgroundColor = [UIColoryellowColor];

    view.hidden =YES;

    UIButton *button0 = [[UIButtonalloc]initWithFrame:CGRectMake(0,0, 150,300)];

    [button0 addTarget:selfaction:@selector(button0Action:)forControlEvents:UIControlEventTouchUpInside];

    [view addSubview:button0];

    

    

    UIButton *button1 = [[UIButtonalloc]initWithFrame:CGRectMake(view.bounds.size.width/2-50,5, 100, 25)];

//    button1.backgroundColor = [UIColor redColor];

    

    [button1 setTitle:@"热门微博"forState:UIControlStateNormal];

    [button1 addTarget:selfaction:@selector(button1Action:)forControlEvents:UIControlEventTouchUpInside];

    [button1 setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];

    [view addSubview:button1];

    

    UIButton *button2 = [[UIButtonalloc]initWithFrame:CGRectMake(view.bounds.size.width/2-50,35, 100, 25)];

    [button2 setTitle:@"我的资料"forState:UIControlStateNormal];

    [button2 addTarget:selfaction:@selector(button2Action:)forControlEvents:UIControlEventTouchUpInside];

    [button2 setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];

    [view addSubview:button2];

    

    UIButton *button3 = [[UIButtonalloc]initWithFrame:CGRectMake(view.bounds.size.width/2-50,65, 100, 25)];

    [button3 setTitle:@"新的信息"forState:UIControlStateNormal];

    [button3 addTarget:selfaction:@selector(button3Action:)forControlEvents:UIControlEventTouchUpInside];

    [button3 setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];

    [view addSubview:button3];

    

    

    

//    view.tag = 1000;

//    [self.view addSubview:view];   //会覆盖

    

    //拿到window

    UIWindow *window = [[UIApplicationsharedApplication].windowsobjectAtIndex:0];  //拿到window

    [window addSubview:view];

    //这个view出了这个viediload后就会消亡,所以要设成全局变量才可以

    NSLog(@"ttttt");//用来测试消亡

   

    //导航栏的左边存在BarButtonItem

    UIBarButtonItem *barButtonItem = [[UIBarButtonItemalloc]initWithTitle:@"返回"style:UIBarButtonItemStylePlaintarget:nilaction:nil];

//    self.navigationItem.leftBarButtonItem = barButtonItem;

    

    UIBarButtonItem *barButtonItem1 = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAddtarget:nilaction:nil];  //item里面有好多类型

//    self.navigationItem.rightBarButtonItem = barButtonItem1;

    

//    self.navigationItem.rightBarButtonItems = @[barButtonItem1,barButtonItem];

    

    /*

     设置item的间距

     UIBarButtonSystemItemFlexibleSpace, 灵活的

     UIBarButtonSystemItemFixedSpace,固定的

     

     */

    

    

    

    

    UIBarButtonItem *barButtonItem2 = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpacetarget:nilaction:nil];

    

    //设置间距为60

//    barButtonItem3.width = 60;

    

//    self.navigationItem.rightBarButtonItems = @[barButtonItem,barButtonItem2,barButtonItem1];

    //这时候设置间距无效果

    

    

//    UIBarButtonItem *barButtonItem3 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];

    

    //设置间距为60

//    barButtonItem3.width = 60;

    

//    self.navigationItem.rightBarButtonItems = @[barButtonItem,barButtonItem3,barButtonItem1];

    

//    UIBarButtonItem *barButtonItem4 = [UIBarButtonItem alloc]initWithCustomView:<#(nonnull UIView *)#>

    

    

//    UIBarButtonItem *barButtonItem5 = [UIBarButtonItem alloc]initWithImage:<#(nullable UIImage *)#> style:<#(UIBarButtonItemStyle)#> target:<#(nullable id)#> action:<#(nullable SEL)#>  //对应image30*30漏空的

    

    

    

    //UIToolbar

//    UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 200, [UIScreen mainScreen].bounds.size.width, 44)];

//    

//    toolBar.items = @[barButtonItem,barButtonItem2,barButtonItem1];

////    [self.view addSubview:toolBar];

//    

//    UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(20, 140, [UIScreen mainScreen].bounds.size.width, 30)];

//    textField.placeholder = @"placeHolder";

//    textField.inputAccessoryView = toolBar;

//    

//    

//    textField.delegate = self;

//    [self.view addSubview:textField];

    

    

    

    //toolBarItems添加元素

    self.toolbarItems =@[barButtonItem,barButtonItem2,barButtonItem1];

    

    

    

    

    

    

}


-(void)viewWillDisappear:(BOOL)animated

{

    NSLog(@"pppppp");

}




#pragma mark ~~~~~~~~~~~~~方法实现~~~~~~~~~~~~~~~~~


-(void)pushButtonAction:(UIButton *)sender

{

    AViewController *aViewController = [[AViewControlleralloc]init];

    

    //模态视图推送功能

//    [self presentViewController:aViewController animated:YES completion:^{

//        

//    }];   //这样过去那个视图就没有导航控制器了

    

    //当视图控制器被压入导航控制器中,就具备自动返回    //:先进后出,压下去再挤底下的出

    [self.navigationControllerpushViewController:aViewController animated:YES];

    

    

}



-(void)titleBtnAction:(UIButton *)sender

{

//   UIView *view = (UIView *)[self.view viewWithTag:1000];

    view.hidden = !view.hidden;

    NSLog(@"touch me");

}




#pragma mark --------UITextField---------

-(BOOL)textFieldShouldReturn:(UITextField *)textField

{

    [textField resignFirstResponder];

    return YES;

}



#pragma mark ~~~~~~~~~button1Action:~~~~~~~~~~~~~

-(void)button1Action:(UIButton *)sender

{

    /*

    NSArray *allViewsArray = self.navigationController.viewControllers;

    for (UIViewController *viewController in allViewsArray) {

        if ([viewController isKindOfClass:[AViewController class]]) {

            [self.navigationController popToViewController:viewController animated:YES];

        }

        

    }

     //如果一开始就运行这个的话,是不会跳转到相应页面的,因为这里用的是pop,pop的意思就是在创建view后压到栈里面后在里面找,如果用的是push的话就是创建并压进去.

        */

    

    BViewController *bViewController = [[BViewControlleralloc]init];

    [self.navigationControllerpushViewController:bViewController animated:YES];

    

    

    view.hidden = !view.hidden;

}


-(void)button2Action:(UIButton *)sender

{

    AViewController *aViewController = [[AViewControlleralloc]init];

    [self.navigationControllerpushViewController:aViewController animated:YES];

    view.hidden = !view.hidden;

}


-(void)button3Action:(UIButton *)sender

{

    CViewController *cViewController = [[CViewControlleralloc]init];

    [self.navigationControllerpushViewController:cViewController animated:YES];

    view.hidden = !view.hidden;

}



-(void)button0Action:(UIButton *)sender

{

    

    view.hidden = !view.hidden;

}



- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end



//跳转到以下:

//AViewController.m

//

//  AViewController.m

//  Tank_UI_07

//

//  Created by ibokan on 16/1/13.

//  Copyright © 2016谭其伟. All rights reserved.

//


#import "AViewController.h"

#import "BViewController.h"

#import "PushView.h"




@interface AViewController ()


@end


@implementation AViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    

    self.view.backgroundColor = [UIColorredColor];

    

    

    PushView *pushView = [[PushViewalloc]initWithFrame:CGRectMake([UIScreenmainScreen].bounds.size.width/2-30, [UIScreenmainScreen].bounds.size.height/2-15,60,30)];

    

    [pushView.pushButtonaddTarget:selfaction:@selector(pushButtonAction:)forControlEvents:UIControlEventTouchUpInside];

    

    self.title =@"我的资料";

    

    [self.viewaddSubview:pushView];

    

    

    

    

}


-(void)pushButtonAction:(UIButton *)sender

{

    BViewController *bViewController = [[BViewControlleralloc]init];

    

    

    [self.navigationControllerpushViewController:bViewController animated:YES];

    

    





}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end



BViewController.m

//

//  BViewController.m

//  Tank_UI_07

//

//  Created by ibokan on 16/1/13.

//  Copyright © 2016谭其伟. All rights reserved.

//


#import "BViewController.h"

#import "PushView.h"

#import "CViewController.h"


@interface BViewController ()


@end


@implementation BViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    

    

    

    self.view.backgroundColor = [UIColororangeColor];

    

    self.title =@"热门微博";

    

    

    

    PushView *pushView = [[PushViewalloc]initWithFrame:CGRectMake([UIScreenmainScreen].bounds.size.width/2-30, [UIScreenmainScreen].bounds.size.height/2-15,60,30)];

    

    [pushView.pushButtonaddTarget:selfaction:@selector(pushButtonAction:)forControlEvents:UIControlEventTouchUpInside];

    

    [self.viewaddSubview:pushView];

    

    

}


-(void)pushButtonAction:(UIButton *)sender

{

    CViewController *cViewController = [[CViewControlleralloc]init];

    

    

    [self.navigationControllerpushViewController:cViewController animated:YES];

    

    

    

    

    

    

}





- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end



CViewController.m


//

//  CViewController.m

//  Tank_UI_07

//

//  Created by ibokan on 16/1/13.

//  Copyright © 2016谭其伟. All rights reserved.

//


#import "CViewController.h"

#import "BViewController.h"

#import "PushView.h"

#import "AViewController.h"



@interface CViewController ()


@end


@implementation CViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    

    

    

    self.title =@"新的信息";

    self.view.backgroundColor = [UIColorwhiteColor];

    

    

    

}



#pragma mark  -------------关联---------


- (IBAction)pop:(id)sender {

    

    /*

    AViewController *aViewContrller = [[AViewController alloc]init];

    NSLog(@"%@",aViewContrller);

     //Push的话:是把新创建的一个又压到栈里面里,然后直接跳到新的那个,只是新的那个跟原来的是一样的

    [self.navigationController pushViewController:aViewContrller animated:YES];

     //Popto的话是在栈里面找所有的view;所以当用Pop找的时候就找不到了

    [self.navigationController popToViewController:aViewContrller animated:YES];

    

     */

    

    //返回上一个页面:

//    [self.navigationController popViewControllerAnimated:YES];

    

    //返回指定页面:

    //拿到当前栈所有的viewControllers

    NSArray *allViewsArray =self.navigationController.viewControllers;

    //遍历

    for (UIViewController *viewControllerin allViewsArray) {

        //isKindOfClass检测对象是否是该类或派生类的成员(ismember就只能是本类)

        if ([viewController isKindOfClass:[AViewController class]]) {

            //返回到指定页面

            [self.navigationControllerpopToViewController:viewController animated:YES];

        }

    }

    

    

    

    

}



- (IBAction)popToRoot:(id)sender {

    

    [self.navigationControllerpopToRootViewControllerAnimated:YES];

    

    

    

}



- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end



//Push类,封装一个Button

//



//封装一个button


//  PushView.h

//  Tank_UI_07

//

//  Created by ibokan on 16/1/13.

//  Copyright © 2016谭其伟. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface PushView : UIView


@property (nonatomic,strong)UIButton *pushButton;


-(instancetype)initWithFrame:(CGRect)frame;




@end



//

//  PushView.m

//  Tank_UI_07

//

//  Created by ibokan on 16/1/13.

//  Copyright © 2016谭其伟. All rights reserved.

//


#import "PushView.h"


@implementation PushView






-(instancetype)initWithFrame:(CGRect)frame

{

    if (self = [superinitWithFrame:frame]) {

        self.pushButton = [UIButtonbuttonWithType:UIButtonTypeCustom];

        [self.pushButtonsetTitle:@"push"forState:UIControlStateNormal];

        [self.pushButtonsetTitleColor:[UIColorpurpleColor] forState:UIControlStateNormal];

        self.pushButton.backgroundColor = [UIColorgreenColor];

        self.pushButton.translatesAutoresizingMaskIntoConstraints = NO;

        [self addSubview:self.pushButton];

        

        

        NSLayoutConstraint *con1 = [NSLayoutConstraintconstraintWithItem:self.pushButtonattribute:NSLayoutAttributeCenterXrelatedBy:NSLayoutRelationEqualtoItem:selfattribute:NSLayoutAttributeCenterXmultiplier:1constant:0];

        

        NSLayoutConstraint *con2 = [NSLayoutConstraintconstraintWithItem:self.pushButtonattribute:NSLayoutAttributeCenterYrelatedBy:NSLayoutRelationEqualtoItem:selfattribute:NSLayoutAttributeCenterYmultiplier:1constant:0];

        

        NSLayoutConstraint *con3 = [NSLayoutConstraintconstraintWithItem:self.pushButtonattribute:NSLayoutAttributeWidthrelatedBy:NSLayoutRelationEqualtoItem:selfattribute:NSLayoutAttributeWidthmultiplier:1constant:0];

        

        NSLayoutConstraint *con4 = [NSLayoutConstraintconstraintWithItem:self.pushButtonattribute:NSLayoutAttributeHeightrelatedBy:NSLayoutRelationEqualtoItem:selfattribute:NSLayoutAttributeHeightmultiplier:1constant:0];

        //如果是nil则会充满整个屏幕

        //self后则self.pushButton会跟 Pushview相同大小

        

        [self addConstraint:con1];

        [self addConstraint:con2];

        [self addConstraint:con3];

        [self addConstraint:con4];


        

        

        

        

        

        

    }

    return self;

}












@end













0 0
原创粉丝点击