你的位置:首页 > 操作系统

[操作系统]导航栏设置多个按钮、背景图、图片与文字按钮方法小结


设置一个按钮的方法就不再赘述了

  1. 设置多个按钮

法一:

    UIBarButtonItem *oneButton = [[UIBarButtonItemalloc]initWithTitle:@"设置" style:UIBarButtonItemStylePlain

target:selfaction:@selector(oneButtonAction:)];

    UIBarButtonItem *twoButton = [[UIBarButtonItemalloc]initWithTitle:@"消息" style:UIBarButtonItemStyleDone target:selfaction:@selector(twoButtonAction:)];

    NSArray *actionButtonItems = @[oneButton,   twoButton];

    self.navigationItem.rightBarButtonItems = actionButtonItems;

注:以上的style参数,我写的是UIBarButtonItemStylePlain和UIBarButtonItemStyleDone两种方式,UIBarButtonItemStyleDone默认字体为系统按钮的字体的设置(加粗),UIBarButtonItemStylePlain默认字体为lable的字体设置(17号字,无加粗)

 

法二:(此法本人不推荐,因为在点击时,两个按钮的背景(即UIToolbar的背景)都会有常亮显示)

    UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, WIDTHINIPHONE6(95), 44)];

    [tools setBarTintColor:DOMINANTCOLOR];

    [tools setTintColor:WHITEBACK];

    [tools setAlpha:[self.navigationController.navigationBar alpha]];

    UIBarButtonItem *setBtn = [[UIBarButtonItem alloc] initWithTitle:@"设置" style:UIBarButtonItemStylePlain target:self action:@selector(setUserSetting)];

    UIBarButtonItem *msgBtn = [[UIBarButtonItem alloc] initWithTitle:@"消息" style:UIBarButtonItemStylePlain target:self action:@selector(lookUserMsg)];

    NSArray *btnArr = @[setBtn,msgBtn];

    [tools setItems:btnArr];

    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithCustomView:tools];

 

      2.  背景图

 [navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];

      3.  图片与文字按钮(这里我以自定义左部按钮为例)

    _leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _leftBtn.frame = CGRectMake(0, 0, 60, 25);
    [_leftBtn setTitle:@"返回" forState:UIControlStateNormal];
    [_leftBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    _leftBtn.titleLabel.font = HEADFONT;
    [_leftBtn addTarget:self action:@selector(goback) forControlEvents:UIControlEventTouchUpInside];
    _leftBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
    _leftBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:_leftBtn];
    self.navigationItem.leftBarButtonItem = leftButton;

 

参考:http://blog.csdn.net/lengshengren/article/details/16820803