具有自定义 UIButton 的 UIBarButtonItem 在 iOS 上不可见<= 10



我需要在导航控制器的左侧为我的应用程序创建一个"汉堡菜单"按钮,但由于NavCon是透明的,我需要在图标上有一个阴影。

因此,我创建了一个带有图像、阴影的自定义UIButton,并将其添加为UIBarButtonItem上的自定义视图,如下所示:

let menuButton = UIButton(type: .custom)
menuButton.addTarget(self, action: #selector(showSideMenu), for: .touchUpInside)
menuButton.setImage(#imageLiteral(resourceName: "menu_white"), for: .normal)
menuButton.tintColor = UIColor.white
menuButton.layer.masksToBounds = false
menuButton.layer.shadowColor = UIColor.black.cgColor
menuButton.layer.shadowOpacity = 1.0
menuButton.layer.shadowRadius = 5
menuButton.layer.shadowOffset = CGSize(width: 0.0, height: 1.0)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: menuButton)

上面的代码在iOS 11上运行得很好,但当我在iOS 9和10(包括模拟器和真实设备(上测试我的应用程序时,菜单图标是不可见的。它是可点击的,工作正常,但没有可见的图标。

在View Hierarchy Debugger中,我可以看到一个宽度和高度为0的UIButton,而在ios 11中,我可以使用嵌入的UIButton看到普通的UIButtonBarStackwiew。

关于如何解决这个问题以及为什么会发生这种情况,有什么想法吗?非常感谢!

请提及按钮框架

let menuButton =  UIButton(frame: CGRect(x: 0, y: 0, width: 70, height: 40))

它可能会帮助你谢谢

您只需调用menuButton.sizeToFit((就可以了。

相关内容

最新更新