我想要一个自定义的 titleView 来填充除 BarButtonItems 之外的有用空间。
@property (strong, nonatomic) IBOutlet UIView *testView;
在 ViewDidLoad
_testView.frame=CGRectMake(0, 0, self.view.frame.size.width-self.navigationItem.leftBarButtonItem.width - self.view.layoutMargins.right - self.view.layoutMargins.left, 44);
self.navigationItem.titleView =_testView;
但事情并没有像我预期的那样工作:
几个问题:
iPhone 6S Plus,在给navigationItem.titleView赋值_testView之前,宽度是398,但是viewDidAppear之后就变成了329,这是为什么呢?
titleView和右边框之间有空隙,有什么办法可以消除这个空隙?
对于只有自动生成的后退按钮的页面,我怎样才能得到它的宽度?
更新:感谢@JoBu1324 的帖子,左侧 barButton 的宽度可以在 viewDidAppear 而不是 viewDidLoad 通过以下方式获取:
UIView *leftBarButtonView = [self.navigationItem.leftBarButtonItem valueForKey:@"view"];
CGFloat leftBarButtonWidth = leftBarButtonView ? [leftBarButtonView frame].size.width : (CGFloat)0.0;
也可以使用 leftBarButtonView.frame.origin.x 获取左边距。
我不清楚的是右边距(在我的例子中是 12px)以及 leftBarButtonView 和 titleView 之间的差距,在我的例子中是 6px。