swift - 自定义 NavigationbarButtonItem 显示在错误的位置
问题描述
我有一个名为banditButton 的UIbutton。我希望将此按钮设置为导航栏右侧的导航栏按钮项目。
我尝试将按钮设置如下。但是按钮被放置在屏幕的左上角。我正在使用 banditButton.frame 来调整按钮的大小。我给出的 X 和 Y 坐标为 0,但我没有覆盖要放置为 rightBarButtonItem 的按钮的位置吗?
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
banditButton.frame = CGRect(x: 0,
y: 0,
width: size, height: size)
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: banditButton)
}
另一个问题是:我创建了一个名为“ profileButtonAndUsernameLabel ”的容器视图,它是 UIView()。这个 UIView 有 2 个子视图,分别称为“ profileButton ”和“ usernameLabel ”。此 UIView 应作为leftBarButtonItem执行。但是当下面的代码启动时,我的 UIView 不会显示在屏幕上的任何位置。基本上我希望这个 UIView 像Instagram Posts 中的 ProfilePicture 和 Username。头像在左边,用户名在右边。但这应该作为一个navigationLeftBarButtonItem。不知道我错过了什么。
提前致谢!
profileButton.layer.cornerRadius = 25
profileButton.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
usernameLabel.frame = CGRect(x: profileButtonAndUsernameLabel.left + 55, y: profileButtonAndUsernameLabel.center.y, width: profileButtonAndUsernameLabel.width, height: profileButtonAndUsernameLabel.height)
profileButtonAndUsernameLabel.addSubview(profileButton)
profileButtonAndUsernameLabel.addSubview(usernameLabel)
usernameLabel.text = "Username"
if isPostInMusicGenre == false {
//ProfileButton
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: profileButtonAndUsernameLabel)
}
解决方案
推荐阅读
- r - 拆分字符串但忽略给定字符包围的分隔符
- javascript - 从窗口中选择的文本是空的
- swashbuckle - 为什么 Swashbuckle 会妨碍每次通话?
- php - 电子邮件中的类以某些代码为前缀
- typescript - 从其他属性中确定属性类型
- python-3.x - 如何使用 python 和 amazon-textract 在 PDF 中检索表单数据(键值对)?
- ios - 是否可以告诉 Apple TV 启动应用程序或从 iOS 手机应用程序打开商店?
- excel - 更改工作簿名称的一部分并将文件另存为新名称
- mysql - SQL:比较 2 个表并返回任何不匹配的记录
- python - 如何将自然对数应用于矩阵并在矩阵条目为零时获得零