Настройка rightBarButtonItem в навигационной панели

Я застрял на этой ошибке в течение нескольких дней. У меня проблема с rightBarButtonItem. Я хочу создать собственный вид. Внутри пользовательского вида есть 2 элемента: label, imageView. Когда я набираю это:

let customView = UIView()
    customView.translatesAutoresizingMaskIntoConstraints = false
    customView.backgroundColor = .black
    customView.frame = CGRect(x: 0, y: 0, width: 90, height: 40)

    let coinLabelNavigationBar = UILabel()
    coinLabelNavigationBar.frame = CGRect(x: 40, y: 0, width: 30, height: 25)
    coinLabelNavigationBar.translatesAutoresizingMaskIntoConstraints = false
    coinLabelNavigationBar.text = "200"
    coinLabelNavigationBar.font = UIFont(name: ".SFUIText-Medium", size: 20)
    coinLabelNavigationBar.textColor = UIColor.white

    let coinImage = UIImageView()
    coinImage.frame = CGRect(x: 75, y: 0, width: 25, height: 25)
    coinImage.translatesAutoresizingMaskIntoConstraints = false
    coinImage.image = UIImage(named: "Coin")

    customView.addSubview(coinLabelNavigationBar)
    customView.addSubview(coinImage)

    let rightBarButton = UIBarButtonItem(customView: customView)

    navigationItem.rightBarButtonItem = rightBarButton

Это не работает, навигация Бар показывает мне это: NavBar

У вас есть идеи, как это исправить?

Спасибо за ваши ответы.

2 ответа

Решение

Попробуй это -

let customView = UIView()
//        customView.translatesAutoresizingMaskIntoConstraints = false
    customView.backgroundColor = .black
    customView.frame = CGRect(x: 0, y: 0, width: 90, height: 40)

    let coinLabelNavigationBar = UILabel()
    coinLabelNavigationBar.frame = CGRect(x: 40, y: 0, width: 30, height: 25)
//        coinLabelNavigationBar.translatesAutoresizingMaskIntoConstraints = false
    coinLabelNavigationBar.text = "200"
    coinLabelNavigationBar.font = UIFont(name: ".SFUIText-Medium", size: 20)
    coinLabelNavigationBar.textColor = UIColor.white

    let coinImage = UIImageView()
    coinImage.frame = CGRect(x: 75, y: 0, width: 25, height: 25)
 //        coinImage.translatesAutoresizingMaskIntoConstraints = false
    coinImage.image = UIImage(named: "coin.jpg")

    customView.addSubview(coinLabelNavigationBar)
    customView.addSubview(coinImage)

    let rightBarButton = UIBarButtonItem(customView: customView)

    navigationItem.rightBarButtonItem = rightBarButton

Для Swift 3.0:

let btn1 = UIButton(type: .custom)
btn1.setImage(UIImage(named: "imagename"), for: .normal)
btn1.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
btn1.addTarget(self, action: #selector(Class.Methodname), for: .touchUpInside)
let item1 = UIBarButtonItem(customView: btn1)

let btn2 = UIButton(type: .custom)
btn2.setImage(UIImage(named: "imagename"), for: .normal)
btn2.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
btn2.addTarget(self, action: #selector(Class.MethodName), for: .touchUpInside)
let item2 = UIBarButtonItem(customView: btn2)  

self.navigationItem.setRightBarButtonItems([item1,item2], animated: true)
Другие вопросы по тегам