导航栏UINavigationController

一、设置导航栏背景全透明

-(void)viewWillAppear:(BOOL)animated{

    //设置导航栏背景图片为一个空的image,这样就透明了

    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];

    //去掉透明后导航栏下边的黑边

    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];

}

- (void)viewWillDisappear:(BOOL)animated{

    //    如果不想让其他页面的导航栏变为透明 需要重置

    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];

    [self.navigationController.navigationBar setShadowImage:nil];

}

二、iOS中设置导航栏标题的字体颜色和大小

[self.navigationController.navigationBar setTitleTextAttributes:

@{NSFontAttributeName:[UIFont systemFontOfSize:19],

NSForegroundColorAttributeName:[UIColor redColor]}];

三、不同条件界面的跳转

1、导航控制器的push和pop

导航控制器的跳转方式,首先我们在appDelegate中设置其中一个viewController为导航控制器的根视图控制器,再把这个导航控制器设置为window的根视图控制器

AppDelegate.m//

//生成一个viewController对象

ViewController *rootVC = [[ViewController alloc]init];

//把这个对象设置为导航控制器的根视图

UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController:rootVC];

//将这个导航控制器设置为整个工程的window的根视图控制器

self.window.rootViewController = naVC;

然后我们就可以在这个viewController里跳转到其他界面了:[self.navigationController pushViewController:secondVC animated:YES];

然后要返回的话在那个界面安排一个回调方法,代码可以写:[self.navigationController popViewControllerAnimated:YES];这样就直接返回跳转之前的界面了

2、模态

模态比较简单,从这个界面跳到下个界面时我们直接用以下代码,直接让它present出来就可以了。

[self presentViewController:showVC animated:YES completion:nil];

从新显示的界面回来的时候用以下代码,把它自己dismiss掉就可以了

[self dismissViewControllerAnimated:YES completion:nil];

如果既想用模态的方式又想让第二界面的导航栏存在,那么在模态时把它添加进导航控制器就行了。

//实例化一个第二界面对象

SecondViewController *SecondVC= [[SecondViewController alloc]init];

UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController: SecondVC];

[self.navigationController presentViewController:naVC animated:YES completion:nil];

3、Storyboard的segues方式跳转

此方法仅适用于Storyboard中各个页面连线后的跳转,鼠标点击viewControlller,按住control键拖拽到另一个View页面,在弹出的segue页面中选择跳转模式即可,连线完之后选中连线,在Identifier填上对应的标示,然后再在需要跳转的地方实现如下代码即可:

[self performSegueWithIdentifier:@"test" sender:self];

四、让UIView中的Button点击之后跳转到另一个ViewController上去

如果使用导航

第一个按钮方法:

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

第二个按钮方法:

[self.navigationController popViewControllerAnimated:YES];

如果使用模态

第一个按钮方法:

[self presentViewController:secondVC animated:YES completion:nil];

第二个按钮方法:

[self dismissViewControllerAnimated:YES completion:nil];

返回

[self.navigationController popViewControllerAnimated:YES];

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容