gpt4 book ai didi

ios - 为什么自定义导航栏按钮图像会被拉伸(stretch)?

转载 作者:可可西里 更新时间:2023-11-01 04:23:09 28 4
gpt4 key购买 nike

我尝试添加带有自定义图像的导航栏按钮。然而,无论我使用什么方法,图像总是显得拉伸(stretch)。

方法一:

    let barbuttonitem = UIBarButtonItem(image: UIImage(named: "apps_small"), style: .plain, target: self, action: nil)
navigationItem.leftBarButtonItem = barbuttonitem

看起来像这样:

Method 1

方法二:

    let button = UIButton(type: .custom)
button.setImage(UIImage(named: "apps_small"), for: .normal)
button.addTarget(self, action: #selector(TeachingRootViewController.appsTouched), for: .touchUpInside)
button.frame = CGRect(x: 0, y: 0, width: 10, height: 10)
button.bounds = CGRect(x: 0, y: 0, width: 10, height: 10)
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: button)

看起来像这样:

Method 2

方法三:

    let button = UIButton(type: .custom)
button.setImage(UIImage(named: "apps_small"), for: .normal)
button.setTitle("Button", for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 10, height: 10)
button.bounds = CGRect(x: 0, y: 0, width: 10, height: 10)
button.imageEdgeInsets = .init(top: 5, left: 5, bottom: 5, right: 300)
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: button)
navigationItem.leftBarButtonItem?.imageInsets = .init(top: 5, left: 5, bottom: 5, right: 300)

看起来像这样:

Method 3

如您所见,标题不见了。

在 UI 层次结构中,它看起来像这样:

UI Hierarchy

看起来按钮已经占据了导航栏中的所有空间。

但是,带有系统项的按钮没有问题:

System item

有谁知道这个问题的原因吗?我想我的想法已经用完了。

最佳答案

以上答案是一种解决方法,而不是正确的解决方法。正确答案是您应该生成尺寸正确的照片

  • asset@1x: 22x22px
  • asset@2x: 44x44px
  • asset@3x: 66x66px

关于ios - 为什么自定义导航栏按钮图像会被拉伸(stretch)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45515509/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com