rightBarButtonItem 出现在中间
Posted
技术标签:
【中文标题】rightBarButtonItem 出现在中间【英文标题】:rightBarButtonItem appearing in the middle 【发布时间】:2019-08-09 18:13:12 【问题描述】:rightbarbuttonitem 没有出现在导航栏的右侧。我希望导航栏看起来类似于“App Store”中的导航栏
我已经尝试在情节提要和代码中这样做,设置图像内容模式,剪切到边界,并给它一个框架。
我也一直在网上寻找解决方案,但没有一个对我有用。任何帮助或建议将不胜感激,谢谢。
以下是一些截图:
import UIKit
class KYSearchBarController: UISearchController
override init(searchResultsController: UIViewController?)
super.init(searchResultsController: searchResultsController)
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?)
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
required init?(coder aDecoder: NSCoder)
super.init(coder: aDecoder)
// Call in view did appear
func CustomizeSearchBar()
// Changing color of text in textfield.
let textfieldInsideBar = self.searchBar.value(forKey: "searchField") as? UITextField
textfieldInsideBar?.textColor = .darkGray
// Chaning placeholder
let textfieldLbl = textfieldInsideBar?.value(forKey: "placeholderLabel") as? UILabel
textfieldLbl?.textColor = .darkGray
textfieldLbl?.textAlignment = .center
// Icon customization
let glassIcon = textfieldInsideBar?.leftView as? UIImageView
glassIcon?.image = #imageLiteral(resourceName: "icon")
glassIcon?.image?.withRenderingMode(.alwaysTemplate)
glassIcon?.tintColor = .darkGray
// Centering textfield text
textfieldInsideBar?.textAlignment = .center
let clearButton = textfieldInsideBar?.value(forKey: "clearButton") as! UIButton
clearButton.setImage(UIImage(named: "icon1"), for: .normal)
clearButton.tintColor = .darkGray
extension UIView
func MakeRound()
self.layer.cornerRadius = self.frame.width / 5.0
class ViewController: UIViewController, UISearchBarDelegate
let searchController = KYSearchBarController(searchResultsController: nil)
override func viewDidLoad()
super.viewDidLoad()
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
let userimage = UIImageView(image: UIImage(named: "person1"))
userimage.frame = CGRect(x: 60, y: 0, width: 50, height: 50)
userimage.clipsToBounds = true
userimage.layer.masksToBounds = true
userimage.contentMode = .scaleAspectFit
userimage.MakeRound()
let rightBarButton = UIBarButtonItem(customView: userimage)
navigationItem.rightBarButtonItem = rightBarButton
override func viewDidAppear(_ animated: Bool)
super.viewDidAppear(true)
searchController.CustomizeSearchBar()
【问题讨论】:
您愿意分享当前对此问题的看法吗? @FaysalAhmed 你是什么意思?这是项目中唯一的视图。默认视图控制器 谢谢,但这也不起作用@FaysalAhmed。 【参考方案1】:-
添加
userimage
属性使其可在 ViewController 中访问。
class ViewController: UIViewController, UISearchBarDelegate
let searchController = KYSearchBarController(searchResultsController: nil)
let userimage = UIImageView(image: UIImage(named: "person1"))
-
将
makeRound()
函数调用添加到viewWillLayoutSubviews()
。
override func viewWillLayoutSubviews()
super.viewWillLayoutSubviews()
userimage.makeRound()
-
更新 makeRound() 函数以制作圆。
extension UIView
func makeRound()
self.layer.cornerRadius = self.frame.width / 2.0
-
添加方法以添加必要的约束。
func setupConstraints()
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
guard let navigationBar = self.navigationController?.navigationBar else return
navigationBar.addSubview(userimage)
userimage.clipsToBounds = true
userimage.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
userimage.rightAnchor.constraint(equalTo: navigationBar.rightAnchor, constant: -16),
userimage.bottomAnchor.constraint(equalTo: navigationBar.bottomAnchor, constant: -12),
userimage.heightAnchor.constraint(equalToConstant: 40),
userimage.widthAnchor.constraint(equalTo: userimage.heightAnchor)
])
-
为 UIImageView 设置手势识别器并为其实现。
func setUpGestureRecognizer()
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(profile))
userimage.isUserInteractionEnabled = true
userimage.addGestureRecognizer(tapGestureRecognizer)
@objc func profile()
// Your implementation
-
使用方法调用更新
viewDidLoad()
。
override func viewDidLoad()
super.viewDidLoad()
// Setup constraints
setupConstraints()
setUpGestureRecognizer()
【讨论】:
【参考方案2】:我在为UIBarButtonItem
使用非常大的图像时遇到了同样的问题。
将图片调整为较小尺寸后,它会适当地放置在导航栏的右侧。看来您遇到了同样的问题。
或者,由于从 ios 11 开始导航栏使用自动布局,替换行
userimage.frame = CGRect(x: 60, y: 0, width: 50, height: 50)
以下也应该可以解决问题:
userimage.widthAnchor.constraint(equalToConstant: 50).isActive = true
userimage.heightAnchor.constraint(equalToConstant: 50).isActive = true
【讨论】:
以上是关于rightBarButtonItem 出现在中间的主要内容,如果未能解决你的问题,请参考以下文章
rightBarButtonItem 没有出现在导航栏 iOS
UINavigationController 并将徽章添加到 rightBarButtonItem