iOS 11 - 如何更改rightBarButtonItems的位置?

时间:2017-12-12 16:31:15

标签: ios objective-c uinavigationitem

iOS 10或更早版本中的代码正常工作

UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setTitle:@"yyyyy"  forState:UIControlStateNormal];
[btn1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[btn1 sizeToFit];
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithCustomView:btn1];

UIBarButtonItem *fixed = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFixedSpace target:nil action:nil];
fixed.width = -22;

self.navigationItem.rightBarButtonItems  = @[fixed, item1];

enter image description here

如果我想在iOS 11中做同样的事情,我能做些什么呢?

1 个答案:

答案 0 :(得分:0)

我们通过继承UINavigationBar来完成这个任务:

#import <UIKit/UIKit.h>

@interface InsetButtonsNavigationBar : UINavigationBar

@end

并设置layoutMargins

#import "InsetButtonsNavigationBar.h"

@implementation InsetButtonsNavigationBar

- (void)layoutSubviews {

    [super layoutSubviews];

    for (UIView * view in self.subviews) {

        view.layoutMargins = UIEdgeInsetsMake(0, 8, 0, 8);

    }

}

@end