iOS 11 - 如何改变rightBarButtonItems的位置?
iOS 11 - how to change the position of rightBarButtonItems?
iOS10 或更早版本中的代码有效
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];
如果我想在 iOS 11 中做同样的事情,我该怎么办?
我们通过子类化 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
iOS10 或更早版本中的代码有效
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];
如果我想在 iOS 11 中做同样的事情,我该怎么办?
我们通过子类化 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