iOS Navigation 使用的一些总结

Posted 其意亦凡

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS Navigation 使用的一些总结相关的知识,希望对你有一定的参考价值。

1.先说添加吧

  AppDelegate.h

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    // 修改导航颜色
    [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:100 green:80 blue:50 alpha:1]];
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    ViewController *vc = [[ViewController alloc] init];
    self.window.rootViewController = vc;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

2.自定义导航栏

系统自带的方法
//左边按钮
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(selectLeftAction:)];
    self.navigationItem.leftBarButtonItem = leftButton;
    
    //右边按钮
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(selectRightAction2:)];
    self.navigationItem.rightBarButtonItem = rightButton;

自定义按钮图案

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[self scaleToSize:[UIImage imageNamed:@"myselect.png"] size:CGSizeMake(20, 20)] style:UIBarButtonItemStylePlain target:self action:@selector(Actionright:)];





//设置图片大小
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{
    // 创建一个bitmap的context
    // 并把它设置成为当前正在使用的context
    UIGraphicsBeginImageContext(size);
    // 绘制改变大小的图片
    [img drawInRect:CGRectMake(0, 0, size.width, size.height)];
    // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    // 返回新的改变大小后的图片
    return scaledImage;
}

设置push返回按钮的样式

 //push返回按钮样式
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = item;
    //self.navigationController.navigationBar.tintColor = [UIColor grayColor];

自定义标题与导航栏的样式

//修改导航栏标题字体大小和颜色,背景颜色
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:212/255.0 green:51/255.0 blue:36/255.0 alpha:1]];
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{
                                                                      NSFontAttributeName:[UIFont systemFontOfSize:17],
                                                                      NSForegroundColorAttributeName:[UIColor whiteColor]
                                                                      
                                                                      }];

技术分享

这是改变最上边电量图标,时间等颜色

 

3.关于跳转的一些总结:

(1).push跳转到下一页,会带着自己导航栏一起跳,这里说的导航栏说的是他自定义的导航栏的属性

NextViewController *next =[[NextViewController alloc]init];
    [self.navigationController pushViewController:next animated:NO];

(2).push跳转到下一页,下一页隐藏导航栏

//隐藏导航栏
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = YES;
}

(3).pop到前面的任何一页面

//返回视图根控制器
    [self.navigationController popToRootViewControllerAnimated:YES];

//pop到指定页面
//    for (UIViewController *controller in self.navigationController.viewControllers) {
//        if ([controller isKindOfClass:[NextViewController class]]) {
//            NextViewController *A =(NextViewController *)controller;
//            [self.navigationController popToViewController:A animated:YES];
//        }
//    }

//其中的NextViewController为想要跳转到的view

(4).present不带导航栏的跳转

HomeViewController *home = [[HomeViewController alloc]init];
    
    [self presentViewController:home animated:YES completion:^{
        
        NSLog(@"Login Success!");
    }];

如果是拖页面的话

HomeViewController *home = [self.storyboard instantiateViewControllerWithIdentifier:@"home"];
    home.sessionID = self.sessionID;
    [self presentViewController:home animated:YES completion:^{
        
        NSLog(@"Login Success!");
    }];

 

以上是关于iOS Navigation 使用的一些总结的主要内容,如果未能解决你的问题,请参考以下文章

Android Jetpack - 使用 Navigation 管理页面跳转

为啥 iOS 11 中 Navigation backBarButtonItem 的位置出现了三个按钮?

Swift3 iOS - Navigation TitleView 中的圆形 ImageView 保持显示正方形?

Linux内核设计第二周学习总结 完成一个简单的时间片轮转多道程序内核代码

springboot报错说 Failed to parse multipart servlet request; nested exception is java.io.IOException(代码片

iOS并发编程对比总结,NSThread,NSOperation,GCD - iOS