navigaiton栏中约束UIBarButtonItem的大小

navigaiton栏中约束UIBarButtonItem的大小

本文介绍了使用iOS 11在navigaiton栏中约束UIBarButtonItem的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我尝试在导航栏中设置具有不同大小图像的UIBarButtonItem。所以我创建了一个基于UIBarButtonItem的自定义视图,并设置自定义视图的框架以约束UIBarButtonItem的宽度。在将软件更新到iOS 11之前,它一直运行良好。设置自定义视图的框架以约束UIBarButtonItem的宽度在iOS 11上似乎不再有用。

I try to set UIBarButtonItem with different size images in the navigation bar. So I create a UIBarButtonItem based custom view and set the custom view's frame to constraint the UIBarButtonItem's width. It had been working well before I updated the software to iOS 11. That set custom view's frame to constraint the UIBarButtonItem's width seems no longer useful on iOS 11.

我使用了image defaultImage 120 * 120:

I used the image defaultImage with 120*120:

UIButton *leftCustomButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];

[leftCustomButton setImage:[UIImage imageNamed:@"defaultImage"] forState:UIControlStateNormal];

UIBarButtonItem * leftButtonItem =[[UIBarButtonItem alloc] initWithCustomView:leftCustomButton];

self.navigationItem.leftBarButtonItems = @[self.headerIconItem];

在iOS10,iOS9上,leftBarButtonItem的图像未被拉伸。它显示如下:

On iOS10, iOS9 the leftBarButtonItem's image is not stretched. It show's like:

但是leftBarButtonItem的图像在iOS11上延伸。它显示在下面的图片中。

But the leftBarButtonItem's image is stretched on iOS11. It show's in the picture below.

是否有一些方法来约束UIBarButtonItem的iOS 11导航栏中的宽度?

Is there have some ways to constraint UIBarButtonItem's width in the navigation bar on iOS 11?

推荐答案

从iOS 11开始 UIBarButtonItem s现在使用自动布局引擎布局,在你的情况下,当你定位iOS 11时,你应该说:

Starting with iOS 11 UIBarButtonItems are now laid out using the auto layout engine, in your case when targeting iOS 11 you should say something like:

UIButton *leftCustomButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];

[leftCustomButton.widthAnchor constraintEqualToConstant:35].active = YES;
[leftCustomButton.heightAnchor constraintEqualToConstant:35].active = YES;

[leftCustomButton setImage:[UIImage imageNamed:@"defaultImage"] forState:UIControlStateNormal];
UIBarButtonItem * leftButtonItem =[[UIBarButtonItem alloc] initWithCustomView:leftCustomButton];
self.navigationItem.leftBarButtonItems = @[leftButtonItem];

有关详细信息,请参阅 WWDC 2017会话。

For more information you should see the Updating Your App for iOS 11 WWDC 2017 session.

这篇关于使用iOS 11在navigaiton栏中约束UIBarButtonItem的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 14:22