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

标签 ios swift uibutton uibarbuttonitem

我使用以下代码创建了一个带有自定义 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/

相关文章:

ios - 使用 SFSpeechRecognizer 的正确方法?

ios - 使用 addSublayer() 添加 CATextLayer

ios - 如何在 UISplitViewController 中命名后退按钮

ios - 跟随圆圈外部路径的 uibuttons 的旋转动画,跟随手指

ios - 如何将用户加入已在 Quickblox 中创建的视频通话 session ?

ios - 带有 WKWebView 的 iOS 14.4 上的 getUserMedia 仍然会产生 NotAllowedError

ios - 应用程序为 "waiting for review"时的试飞

swift - 核心数据: Transformable Type or To-Many Relationship?

ios - 将复选标记图像添加到多个按钮阵列

iphone - 如何在单击 iPhone 中的 UIButton 时打开相机?