ios系列——导航条整理

导航条的操作

  • 导航条的背景设置
//设置导航条的背景颜色
nav.navigationBar.barTintColor = MAINCOLOR;

//设置导航条的背景图片
UINavigationBar *navBar = [UINavigationBar appearance];  
[navBar setBackgroundImage:[UIImage imageNamed:navBarBg] forBarMetrics:UIBarMetricsDefault];  
  • 设置文字颜色
//设置系统级的左按钮右按钮颜色
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
//设置导航文字颜色
[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil]];
  • 设置按钮
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 0, 100, 44)];
    button.backgroundColor = [UIColor blackColor];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:button];
 self.navigationItem.rightBarButtonItem = rightItem;


self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Add" style:UIBarButtonItemStylePlain target:self action:@selector(perFormAdd:)];//为导航栏添加右侧按钮    

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(perFormAdd:)];
  • 设置右边多个按钮
UIBarButtonItem *Item = [[UIBarButtonItem alloc] initWithCustomView:button];
[rigArr addObject:rightItem];
[rigArr addObject:rightItem];
self.navigationItem.rightBarButtonItems = rigArr;
  • 设置一个view到nav上 就可以在view上做很多操作了
UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
buttonView.backgroundColor = [UIColor greenColor];
UIButton *exampleButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
exampleButton2.frame = CGRectMake(70, 2, 30, 40);
[exampleButton2 setImage:[UIImage imageNamed:@"image2.png"] forState:UIControlStateNormal];
exampleButton2.backgroundColor = [UIColor redColor];
[buttonView addSubview:exampleButton2];
[exampleButton2 addTarget:self action:@selector(buttonActivity:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonView];
self.title = @"首页";

    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColor whiteColor], NSForegroundColorAttributeName, nil]];

    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 0, 100, 44)];
    button.backgroundColor = [UIColor blackColor];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    [button addTarget:self action:@selector(buttonActivity:) forControlEvents:UIControlEventTouchUpInside];

    self.navigationItem.rightBarButtonItem = rightItem;

    UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    buttonView.backgroundColor = [UIColor greenColor];

    UIButton *exampleButton1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 2, 30, 40)];
    [buttonView addSubview:exampleButton1];
    exampleButton1.backgroundColor = [UIColor blackColor];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonView];


    UIButton *exampleButton2 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 40)];
    exampleButton2.backgroundColor = [UIColor redColor];
    [exampleButton2 addTarget:self action:@selector(buttonActivity:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.titleView = exampleButton2;

如果设置rightBarButtonItem的宽度小于屏幕一半的宽度
ios系列——导航条整理_第1张图片

如果设置rightBarButtonItem的宽度大于屏幕一半的宽度
ios系列——导航条整理_第2张图片

  • 导航条按钮
    当设置了左边的按钮 这个页面的手势回退就用不了了

你可能感兴趣的:(ios系列——导航条整理)