美文网首页
一.创建导航控制器以及与导航相关一些代码

一.创建导航控制器以及与导航相关一些代码

作者: 择一城终老_蜗牛 | 来源:发表于2016-11-10 16:58 被阅读0次

(1)导航控制器 在APPdelegate中

在appdelegate中:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    
    //1.创建一个导航控制器
    UINavigationController *nav=[[UINavigationController alloc]init];
    //2.设置导航控制器为window的根视图
    self.window.rootViewController=nav;
    
    
    //3.添加子控制器到导航控制器中
   
    ViewController *c1=[[ViewController alloc]init];
    //把这些控制器添加到导航控制器中
    [nav pushViewController:c1 animated:YES];
    [self.window makeKeyAndVisible];
    return YES;
}

(2)添加左右按钮(创建自定义按钮或者系统类型按钮)

  (2.1)自定义的左右按钮
  //选择患者右键
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    [rightButton setTitle:@"添加" forState:UIControlStateNormal];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [rightButton addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = rightItem;


//返回按钮
        UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        leftBtn.frame = CGRectMake(0, 0, 44, 44);
        [leftBtn setImage:[UIImage imageNamed:@"backblack"] forState:UIControlStateNormal];
        [leftBtn addTarget:self action:@selector(back1) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
        self.navigationItem.leftBarButtonItem = item;


//绑定事件
    -(void) click
{
    //根据tag找到按钮
    UIButton *btn = (UIButton *)[self.view viewWithTag:1];
    btn.userInteractionEnabled = YES;
}

(2.2)系统类型的左右按钮

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:nil action:nil];
    ```
(3) 导航控制器里 添加 新的导航控制器

//明确在哪个页面添加 导航 然后将导航根视图赋值给VC
CreateConsultViewController *create = [[CreateConsultViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:create];
// 总的控制器添加子视图控制器
[self addChildViewController:nav];
// 设置导航控制器的页面大小
nav.view.frame = CGRectMake(SCREEN_WIDTH-633+5, 0, 633, SCREEN_HEIGHT-64);
// 将新添导航页面 加在总页面上 显示
[self.view addSubview:nav.view];

(4) 设置导航栏的边框(设置 整个导航页面的边框)

// 设置导航的边框
nav.navigationBar.layer.shadowOffset = CGSizeMake(- 3, 3);
//设置透明度
nav.navigationBar.layer.shadowOpacity = 0.6;
nav.navigationBar.layer.shadowPath = [UIBezierPath bezierPathWithRect:nav.navigationBar.bounds].CGPath;

// 设置导航的边框
NewHZNav.view.layer.shadowOffset = CGSizeMake(-3, 0);
// 设置透明度
NewHZNav.view.layer.shadowOpacity = 0.6;
// 这句加上 渲染没有了
NewHZNav.view.layer.shadowPath = [UIBezierPath bezierPathWithRect:NewHZNav.view.bounds].CGPath;

//四周都有边框的
newNav.view.layer.borderWidth = 3;
newNav.view.layer.borderColor = [[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5 ] CGColor];

(5)导航控制器  跳页相关

// 导航控制器 是一个大数组 index=0是创建导航的那个页面
CreateConsultViewController *create = [self.navigationController.viewControllers objectAtIndex:0];
[self.navigationController popToViewController:create animated:YES];

6)隐藏导航栏
  • (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [self.navigationController setNavigationBarHidden:YES animated:YES];
    }

  • (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    [self.navigationController setNavigationBarHidden:NO animated:YES];
    }

相关文章

网友评论

      本文标题:一.创建导航控制器以及与导航相关一些代码

      本文链接:https://www.haomeiwen.com/subject/efsxpttx.html