UIBarButtonItem不再在iOS 11中显示

时间:2017-09-26 19:58:11

标签: ios objective-c

我刚刚将我的设备更新到iOS以测试我的应用程序以准备iOS 11.我在顶部有一个工具栏,似乎不再有按钮工作 - 图像在iOS 10及更低版本中运行良好。

以下是我初始化图像的方法:

@property (strong, nonatomic) IBOutlet UIToolbar *navBar;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *refreshButton;

    path = [NSString stringWithFormat: @"https://s3.amazonaws.com/myapp/images/refresh.png"];
    url = [NSURL URLWithString:path];
    data = [NSData dataWithContentsOfURL:url];
    refreshButton = [[UIImage alloc] initWithData:data];

    smallerButton = [TRUtils imageWithImage:refreshButton scaledToSize:CGSizeMake(20, 20)];

    //TODO: Check image exists
    self.refreshButton = [[UIBarButtonItem alloc] initWithImage:[smallerButton imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
                                                       style:UIBarButtonItemStylePlain
                                                      target:self
                                                      action: @selector(refreshButtonPushed:)];
    [self.refreshButton setTarget:self];
    self.refreshButton.enabled = YES;
    self.refreshButton.imageInsets = UIEdgeInsetsMake(32, 32, 32, 32);

然后我将它们添加到导航栏:

self.navBar.items = [NSArray arrayWithObjects:self.backButton, self.forwardButton, self.flex, self.toolbarTitle, self.flex, self.refreshButton, self.flex, self.homeButton, nil];

知道为什么图像会在iOS 11中停止显示但在iOS 10中显示得很好吗?

按钮似乎仍然“工作”,但它们只是空白区域,因此人们不知道它们是否可用。

似乎this SO有一个解决方案来修复它。但是当我设置:

self.navigationController.interactivePopGestureRecognizer.delegate = self;

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)] &&
        gestureRecognizer == self.navigationController.interactivePopGestureRecognizer) {
        return NO;
    }
    return YES;
}

问题仍然存在。

1 个答案:

答案 0 :(得分:1)

您在imageInsets上设置的UIBarButtonItem是问题所在。摆脱它们,这应该让图标显示在iOS 11上。鉴于图像的大小和标签栏的正常大小,那里的值似乎是假的。我的猜测是iOS 10完全忽略了它们,但iOS 11正试图做“正确的事情”。

以下是我用来验证iOS 11上的行为的基本操场的内容:

import UIKit
import PlaygroundSupport

extension UIImage {

    func scaledImage(withSize size: CGSize) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
        defer { UIGraphicsEndImageContext() }
        draw(in: CGRect(x: 0.0, y: 0.0, width: size.width, height: size.height))
        return UIGraphicsGetImageFromCurrentImageContext()!
    }    
}

let navbar = UIToolbar(frame: CGRect(origin: .zero, size: CGSize(width: 320, height: 40)))

let imageUrl = URL(string: "http://www.freeiconspng.com/uploads/black-refresh-icon-png-9.png")!
let imageData = try Data(contentsOf: imageUrl)
let image = UIImage(data: imageData)!.withRenderingMode(.alwaysOriginal).scaledImage(withSize: CGSize(width: 20, height: 20))

let shareButton = UIBarButtonItem(barButtonSystemItem: .action, target: nil, action: nil)
let refreshButton = UIBarButtonItem(image: image, style: .plain, target: nil, action: nil)

// Uncomment the line below to reproduce the issue on iOS 11
//refreshButton.imageInsets = UIEdgeInsets(top: 32, left: 32, bottom: 32, right: 32)

navbar.items = [shareButton, refreshButton]

PlaygroundPage.current.liveView = navbar