iOS 11 UIBarButtonItem图像没有大小调整 [英] iOS 11 UIBarButtonItem images not sizing

查看:197
本文介绍了iOS 11 UIBarButtonItem图像没有大小调整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题的答案在



这是他们在Xcode 9.0 GM和iOS 11模拟器中的样子。请注意,顶行按钮调整正确,但底行扩展为填充为标签栏分配的空间。同样的行为也发生在iPad以及各种设备上。





有关如何禁用Autolayout或添加约束的任何想法?

解决方案

BarButtonItem(iOS11 \ xCode9)使用自动布局而不是帧。试试这个(Swift):

  if #available(iOS 9.0,*){
cButton.widthAnchor.constraint( equalToConstant:customViewButton.width).isActive = true
cButton.heightAnchor.constraint(equalToConstant:customViewButton.height).isActive = true
}

目标C

  if(@available(iOS 9,*) ){
[cButton.widthAnchor constraintEqualToConstant:standardButtonSize.width] .active = YES;
[cButton.heightAnchor constraintEqualToConstant:standardButtonSize.height] .active = YES;
}


The answer to my question was hinted at in this question, so I think the answer is to disable autolayout for my UIToolbar view.

The code that is said to work for views is

cButton.translatesAutoresizingMaskIntoConstraints = YES;

But I’m not sure if it applies to my code since UIToolbar doesn’t inherit from UIView.

I have lots of small images that I use in my games that are different sizes depending on the device and orientation. Rather than having lots of different images, and adding new ones when Apple introduces new devices, I decided to make one 160x160 image fore each and then resize it when it is used. This worked fine from iOS 4 - iOS 10 but fails in iOS 11.

The code is pretty straightforward:

// Get the image
NSString *pictFile = [[NSBundle mainBundle] pathForResource:@"Correct" ofType:@"png"];
UIImage *imageToDisplay = [UIImage imageWithContentsOfFile:pictFile];
UIImage *cImage  = [UIImage imageWithCGImage:imageToDisplay.CGImage scale:[UIScreen mainScreen].scale orientation:imageToDisplay.imageOrientation];

UIButton *cButton = [UIButton buttonWithType:UIButtonTypeCustom];
[cButton setImage:cImage forState:UIControlStateNormal];
[cButton setTitle:@"c" forState:UIControlStateNormal];

//set the frame of the button to the size of the image
cButton.frame = CGRectMake(0, 0, standardButtonSize.width, standardButtonSize.height);

//create a UIBarButtonItem with the button as a custom view
c = [[UIBarButtonItem alloc] initWithCustomView:cButton];

This is what it looks like pre11. The bar button items have been resized and fit nicely in the bottom bar. Note I reduced the size of the checkmark by 50% just to make sure I was looking at the correct code and that it behaves like I expect.

Here’s what they look like in the simulator for Xcode 9.0 GM and iOS 11. Note that the top row of buttons resize correctly but the bottom row expand to fill the space allocated for the tab bar. The same behaviour happens on iPads as well and various devices.

Any ideas about how to disable Autolayout or add constraints?

解决方案

BarButtonItem (iOS11\xCode9) uses autolayout instead of frames. Try this (Swift):

if #available(iOS 9.0, *) {
    cButton.widthAnchor.constraint(equalToConstant: customViewButton.width).isActive = true
    cButton.heightAnchor.constraint(equalToConstant: customViewButton.height).isActive = true
}

Objective C

if (@available(iOS 9, *)) {
     [cButton.widthAnchor constraintEqualToConstant: standardButtonSize.width].active = YES;
     [cButton.heightAnchor constraintEqualToConstant: standardButtonSize.height].active = YES;
}

这篇关于iOS 11 UIBarButtonItem图像没有大小调整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆