在iOS 8.2上更改UIBarButtonItem的垂直位置

时间:2015-03-15 12:15:35

标签: ios objective-c uibarbuttonitem

这是我的导航栏

Image link

我在导航栏上更改设置UIBarButtonItem的垂直位置时出现问题。我想将按钮项“设置”向下移动

这是我的代码

 UIBarButtonItem *settingsItem = [[UIBarButtonItem alloc] initWithTitle:@"Settings" style:UIBarButtonItemStylePlain target:self action:nil];
 self.navItem.rightBarButtonItem = settingsItem; 
    [[UIBarButtonItem appearance] setTitlePositionAdjustment:UIOffsetMake(0,-10) forBarMetrics:UIBarMetricsDefault];

我曾经一次又一次地尝试过。它似乎不起作用

有人可以建议我如何将按钮项“设置”向下移动吗?

2 个答案:

答案 0 :(得分:3)

您可以轻松地向NavigationBarItem添加自定义按钮,这是方法,

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; //create custom Button
    [button setTitle:@"Settings" forState:UIControlStateNormal];
    [button.titleLabel setFont:[UIFont systemFontOfSize:14.0]];
    [button setTitleColor:[UIColor colorWithRed:179.0/255.0 green:40.0/255.0 blue:18.0/255.0 alpha:1] forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, 50, 20); //Button frame
    [button addTarget:self action:@selector(yourCustomSelectorHere) forControlEvents:UIControlEventTouchUpInside]; //Add action method to the button here
    UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 63, 33)];
    backButtonView.bounds = CGRectOffset(backButtonView.bounds, -14, -7);
    [backButtonView addSubview:button];
    UIBarButtonItem *barBtnItem = [[UIBarButtonItem alloc]initWithCustomView: backButtonView]; //set button as UIBarButtonItem
    self.navigationItem.rightBarButtonItem = barBtnItem; //set barBtnItem to rightBarButtonItem

通过更改backButtonView.bounds值,您可以更改按钮的来源

答案 1 :(得分:1)

引用苹果关于UIAppearance协议

的文档
  

iOS在视图进入窗口时应用外观更改,但不会   更改已在窗口中的视图的外观。

因此,如果您设置UIBarButtonItem外观的代码位于navigationItem分配代码之后。它不会起作用