iOS导航栏上的设置以及弹出框

来源:互联网 发布:照片编辑软件 编辑:程序博客网 时间:2024/05/22 12:48

1.导航栏上的一些常用属性设置

如果不喜欢系统提供给我们的样式 我们可以自己设置导航栏上面按钮的颜色、字体的颜色和字体大小 

主要写在AppDelegate.m文件里面 导入ViewController.h文件 

它的一些设置 代码如下:

#import "AppDelegate.h"

#import "ViewController.h"


@interface AppDelegate ()


@end


@implementation AppDelegate


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

    self.window = [[UIWindowalloc]initWithFrame:[UIScreenmainScreen].bounds];

    [self.windowmakeKeyAndVisible];

    //UINavigationController:导航控制器

    //初始化导航控制器同时给导航控制器一个根视图控制器根视图控制器是导航控制器上面的 第一个控制器

    UINavigationController *navigationController = [[UINavigationControlleralloc]initWithRootViewController:[[ViewControlleralloc]init]];

    self.window.rootViewController =navigationController;

    

    //设置导航栏的背景颜色

    [[UINavigationBarappearance]setBarTintColor:[UIColororangeColor]];

    //设置导航栏上字体颜色和字体大小

    [[UINavigationBarappearance]setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColorwhiteColor],NSFontAttributeName:[UIFontboldSystemFontOfSize:20]}];

    //设置导航栏上按钮颜色

    [[UINavigationBarappearance]setTintColor:[UIColorgrayColor]];


    return YES;

}

@end


可以看到效果图如下:



2.弹出框的使用 主要通过点击导航栏上面的左右按钮介绍了弹出框的两种不同风格以及可以在弹出框上面添加按钮

代码如下:

#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    self.view.backgroundColor = [UIColorwhiteColor];

    self.title =@"首页";

    

    [selfcreatBarButton];

    

}

#pragma mark---------设置导航栏上左右侧按钮

- (void)creatBarButton{

    

    //创建导航栏上一个左侧按钮导航栏系统自带的按钮风格有24种风格在这里只列举了2

    UIBarButtonItem *left = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearchtarget:selfaction:@selector(action:)];

    self.navigationItem.leftBarButtonItem = left;

    

    //创建导航栏上一个右侧按钮

    UIBarButtonItem *right = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAddtarget:selfaction:@selector(action1:)];

    self.navigationItem.rightBarButtonItem = right;


}

#pragma mark---------导航栏上左右侧按钮的触发事件的方法

- (void)action:(UIBarButtonItem *)sender{

    

    //UIAlertController 弹出框控件 弹出框有2种样式

    //UIAlertController初始化:

    //+ (instancetype)alertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle


    //创建弹出框选择弹出框样式 1.左右结构

    UIAlertController *alertOfStyle = [UIAlertControlleralertControllerWithTitle:@"消息"message:@"加载成功"preferredStyle:UIAlertControllerStyleAlert];

    //弹出框上面的按钮

    UIAlertAction *okAlert = [UIAlertActionactionWithTitle:@"OK"style:UIAlertActionStyleDefaulthandler:nil];

    

    UIAlertAction *cancelAlert = [UIAlertActionactionWithTitle:@"Cancel"style:UIAlertActionStyleCancelhandler:nil];

    

    //把按钮添加到弹出框上面

    [alertOfStyle addAction:okAlert];

    [alertOfStyle addAction:cancelAlert];

    

    [selfpresentViewController:alertOfStyleanimated:YEScompletion:nil];

    

}


- (void)action1:(UIBarButtonItem *)sender{    

    //创建弹出框选择弹出框样式2.上下结构

    UIAlertController *sheetOfStyle = [UIAlertControlleralertControllerWithTitle:@"标题"message:@"选项样式的弹出框"preferredStyle:UIAlertControllerStyleActionSheet];

    

    //弹出框上面的按钮

    UIAlertAction *OK = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDefaulthandler:nil];

    UIAlertAction *ALERT = [UIAlertActionactionWithTitle:@"警告"style:UIAlertActionStyleDestructivehandler:nil];

    

    //把按钮添加到弹出框上面

    [sheetOfStyle addAction:OK];

    [sheetOfStyle addAction:ALERT];

    

    [selfpresentViewController:sheetOfStyleanimated:YEScompletion:nil];


}

@end


可以看到弹出框的两个样式的效果如下:

    


1 0
原创粉丝点击