gpt4 book ai didi

ios - rightBarButtonItem 出现在中间

转载 作者:行者123 更新时间:2023-11-29 05:32:11 27 4
gpt4 key购买 nike

右栏按钮项目未出现在导航栏的右侧。我希望导航栏看起来与“App Store”中的导航栏类似

我尝试在 Storyboard和代码中执行此操作,设置图像内容模式,剪切到边界,并给它一个框架。

我也一直在网上寻找解决方案,但没有一个对我有用。任何帮助或建议将不胜感激,谢谢。

以下是一些屏幕截图: enter image description here enter image description here

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()
}

}


最佳答案

  1. 添加 userimage 属性以使其在 ViewController 内可访问。
class ViewController: UIViewController, UISearchBarDelegate {

let searchController = KYSearchBarController(searchResultsController: nil)

let userimage = UIImageView(image: UIImage(named: "person1"))

}
  1. 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()
    }

    关于ios - rightBarButtonItem 出现在中间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57435274/

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