ios - 调整栏按钮大小时出现问题

标签 ios swift

我在创建一个栏按钮时遇到了一些困难,这让我很困惑。我使用以下代码创建右栏按钮:

let button = UIButton(frame: CGRect(x: 0, y: 0, width: 22, height: 22))
button.setBackgroundImage(UIImage(named: "tune"), for: .normal)
button.addTarget(self, action:#selector(viewController.settingsBtnPressed), for:.touchUpInside)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)

无论我设置什么宽度或高度,按钮尺寸都不会改变。 VC 位于导航 Controller 中。有谁有任何见解或我可以采取的后续步骤?任何帮助将不胜感激

最佳答案

单击 StoryBoard 中的 ViewController。转到“编辑器” -> “嵌入” -> 单击“导航 Controller ”。然后将代码粘贴到 ViewDidLoad 方法中您想在哪里看到 BarButton。

override func viewDidLoad() {
    super.viewDidLoad()
    let button = UIButton(frame: CGRect(x: 0, y: 0, width: 22, height: 22))
    button.setBackgroundImage(UIImage(named: "tune"), for: .normal)
    button.addTarget(self, action:#selector(ViewController.settingsBtnPressed), for:.touchUpInside)
    self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
}

关于ios - 调整栏按钮大小时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48044123/

相关文章:

带有自动高度标签的 Swift tableview 单元格自动高度

iphone - 关闭屏幕播放声音/不要让 iPhone 进入休眠状态

html - iOS:主体固定,div 滚动。如何?

ios - 为什么 UIButton 在禁用时不能保持选中状态?

swift - 解析 Swift 保存多个对象

swift - 循环遍历 Swift 结构以获取键和值

ios - 如何在 UITableView 中使用多个自定义单元格 (>1)

c# - 更改 iOS 设备会使 iOS 应用程序崩溃

ios - 嵌入式 Youtube 视频 - UIWebView 和 Safari 之间的区别

ios - 如何查询特定对象的解析类?