UINavigationItems在iOS11中拉伸

时间:2017-11-02 14:49:38

标签: uinavigationbar ios11 uinavigationitem

我正在更新应用以确保它在iOS11中按预期工作。我遇到的唯一问题是导航栏在iOS11中看起来很不一样。版本之间元素和大小的位置不一样。我查看了我的代码,并明确设置了帧大小和边缘插入,为什么这些值在iOS11中不受尊重?

//-- Create Right Navigation Item Buttons

// create kabob btn
UIButton *kabobBtn =  [UIButton buttonWithType:UIButtonTypeCustom];
[kabobBtn addTarget:self action:@selector(viewUsersClicked:)forControlEvents:UIControlEventTouchUpInside];
kabobBtn.clipsToBounds = YES;    
[kabobBtn setImage:[UIImage imageNamed:@"KabobMenu2_0071bc_Right.png"]
               forState:UIControlStateNormal];
[kabobBtn.imageView setContentMode:UIViewContentModeScaleAspectFit];
[kabobBtn setFrame:CGRectMake(0, 0, 32, 32)];

// create user profile button
UIButton *userBtn =  [UIButton buttonWithType:UIButtonTypeCustom];    
[userBtn setImage:[UIImage imageNamed:@"people.png"]
        forState:UIControlStateNormal];
[userBtn addTarget:self action:@selector(viewUserProfileClicked:)forControlEvents:UIControlEventTouchUpInside];
[userBtn setFrame:CGRectMake(0, 0, 38, 38)];
userBtn.clipsToBounds = YES;

[userBtn.layer setCornerRadius:(38/2)];
[userBtn.layer setMasksToBounds:YES];
[userBtn.layer setBorderColor:[[UIColor blackColor]CGColor]];
[userBtn.layer setBorderWidth:1];


self.userPictureURL = [defaults objectForKey:@"userPicture"];

[userBtn setImage:[UIImage imageNamed:@"defaultUserImage.png"] forState:UIControlStateNormal];
[userBtn setContentMode:UIViewContentModeScaleAspectFill];
[userBtn setBackgroundColor:[UIColor clearColor]];

// add buttons to bar button items
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:kabobBtn];
UIBarButtonItem *barButton2 = [[UIBarButtonItem alloc] initWithCustomView:userBtn];

// add bar button items to right bar button item array
self.navigationItem.rightBarButtonItems = @[barButton,barButton2];

//-- Create Left Hamburger Button
UIButton *customOpen = [UIButton buttonWithType:UIButtonTypeCustom];
[customOpen setFrame:CGRectMake(0, 0, 30, 44)];
[customOpen setImage:[UIImage imageNamed:@"hamburger.png"] forState:UIControlStateNormal];
[customOpen setImageEdgeInsets:UIEdgeInsetsMake(10, -5, 16, 10)];
UIBarButtonItem *openItem = [[UIBarButtonItem alloc] initWithCustomView:customOpen];

if(self.showHamburger){
    self.navigationItem.leftBarButtonItem = openItem;

}else{
    self.navigationItem.leftBarButtonItem = nil;

}

Navigation Bar in iOS10 Navigation Bar in iOS11

1 个答案:

答案 0 :(得分:3)

显然iOS 11使用自动布局用于导航栏按钮。使用自动布局约束来设置按钮宽度和高度。

[userBtn.widthAnchor constraintEqualToConstant:38.f].active = YES;
[userBtn.heightAnchor constraintEqualToConstant:38.f].active = YES;

这应解决拉伸问题。

感谢this answer