gpt4 book ai didi

ios - CustomView UIBarButtonItem 适用于 iPhone 6 以外的所有手机

转载 作者:行者123 更新时间:2023-11-28 15:06:15 25 4
gpt4 key购买 nike

我使用以下代码创建了一个带有自定义 View 的 UIBarButtonItem(我将 profileImageButton 框架的尺寸指定为 40x40)。

let profileImageButton = UIButton(type: .custom)
profileImageButton.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
profileImageButton.setImage(imageCache.getProfileImage(), for: .normal)
profileImageButton.layer.cornerRadius = 20
profileImageButton.layer.masksToBounds = true
profileImageButton.addTarget(self, action: #selector(self.openSettings), for: .touchUpInside)
let settingsButton = UIBarButtonItem(customView: profileImageButton)

在大多数设备上, View 有一个 40x40 的框架。然而,当我在 iPhone 6s 上运行它时,它看起来有点长方形。所以我在 Xcode 的调试层次结构功能中打印出它的框架。我得到的结果是:frame = (0 0; 50 44),我假设这意味着框架的尺寸为 50x44。因此,我的问题是为什么 iPhone 6s 不保持指定的 40x40 尺寸?

最佳答案

在 iOS 11 中,带有自定义 View 的 UIBarButtonItem 的大小由自定义 View 的内部约束决定。你没有提供任何内部约束,所以所有的赌注都没有了;您还没有让自己负责栏按钮项目的大小。为此,给 profileImageButton 一个宽度约束和一个高度约束。

关于ios - CustomView UIBarButtonItem 适用于 iPhone 6 以外的所有手机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48436969/

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