NavigationItem LeftBarButtonItem 自定义视图自动布局

NavigationItem LeftBarButtonItem Custome View Auto Layaut

我正在尝试通过笔尖设置服装 navigationItem.leftBarButtonItem,但横向显示时出现奇怪的行为。

编辑:

要清楚视图包含一个按钮和一个标签,这就是我使用服装视图的原因,按钮和标签都有约束。

self.buttonView =//init;
[self.buttonView setFrame:CGRectMake(0, 0, 36, 36)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.buttonView];

[self.buttonView setBackgroundColor:[UIColor redColor]];
//you will see why

似乎自动布局试图满足一些限制,因此它扩展了您的按钮框架。

要解决此问题,请将您的按钮所需的 contentHugging 优先级设置为:

[self.buttonView setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];

试试看,如果有效请告诉我

这是我能尽快找到的唯一解决方法,如果有人有更好的主意,请随时分享

-(void)viewWillLayoutSubviews{

    [super viewWillLayoutSubviews];

    [self setMenuButtonFrame];

}

-(void)setMenuButtonFrame
{
    self.menuView.frame = CGRectMake(self.menuView.frame.origin.x,     
    self.menuView.frame.origin.y, 
    self.navigationController.navigationBar.frame.size.height-8, 
    self.navigationController.navigationBar.frame.size.height-8);
}