【问题标题】:UIBarButtonItem No Longer Displaying in iOS 11UIBarButtonItem 在 iOS 11 中不再显示
【发布时间】:2017-09-28 03:12:35
【问题描述】:

我刚刚将我的设备更新到 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;
}

问题仍然存在。

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

    您在UIBarButtonItem 上设置的imageInsets 是问题所在。摆脱它们,这应该会让图标出现在 iOS 11 上。考虑到图像的大小和标签栏的正常大小,那里的值似乎是虚假的。我的猜测是 iOS 10 只是忽略了它们,但 iOS 11 正在尝试做“正确的事情”。

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

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-27
      相关资源
      最近更新 更多