使用swift 3在UINavigationController中插入右键图像

时间:2016-11-24 08:34:40

标签: ios swift xcode swift3 uibarbuttonitem

我试图以编程方式覆盖带有静态图像的导航项的rightButton,以便实现此目的:

enter image description here

以下代码不会出现任何错误,但导航栏中不会显示任何内容。

import UIKit
import Foundation
class UICipNavigationController: UINavigationController {

override func viewDidLoad() {
    super.viewDidLoad()


    // Add BPT LOGO as UIBarButton
    let logoBPT = UIImage(named: "Logo BPT")?.withRenderingMode(.alwaysOriginal)

    let logoBPTBarButton = UIBarButtonItem(image: logoBPT, style: .plain, target: nil, action: nil)
    self.navigationItem.rightBarButtonItem = logoBPTBarButton


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

import UIKit

class UICipNavigationBar: UINavigationBar {

override init(frame: CGRect){
    super.init(frame: frame)

}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)!

    self.tintColor = UIColor.white

    self.setBackgroundImage(UIImage.fromColor(color: UIColorFromHex(rgbValue: 0xffffff, alpha: 0.2)), for: UIBarMetrics.default)
    self.shadowImage = UIImage()
    self.isTranslucent = true
    self.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white, NSFontAttributeName: UIFont(name: "Lato-Regular", size: 24)!]

}

override func sizeThatFits(_ size: CGSize) -> CGSize {
    let newSize :CGSize = CGSize(width: self.superview!.bounds.size.width, height: 60)
    return newSize
}

}

我也尝试检查视图,但似乎没有添加任何内容:

enter image description here

4 个答案:

答案 0 :(得分:3)

注意: Image name/file/extention是敏感的。当您设置barButtonImage时。请确保正确设置它们。我正在评估来自asset catalog的图像文件(32pt) )。

 let myimage = UIImage(named: "YourImageFileName")?.withRenderingMode(.alwaysOriginal)
 navigationItem.rightBarButtonItem = UIBarButtonItem(image: myimage, style: .plain, target: self, action: #selector(ButtonTapped))


 func ButtonTapped() { 
 print("Button Tapped")  
}

输出: updated

enter image description here

方法2 Testing Purpose

注意:将您的标题设置为Xcode的表情符号图标。运行项目,看看它有效吗?

    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: self, action: #selector(ButtonTapped))

    func ButtonTapped() { 
    print("Button Tapped")  
   }

<强>输出:

enter image description here

答案 1 :(得分:0)

您应该在导航控制器显示的顶视图控制器中设置右侧栏按钮

答案 2 :(得分:-1)

    let button = UIButton.init(type: .custom)
    button.setImage(UIImage.init(named: "menu"), for: UIControlState.normal)
    button.frame = CGRect.init(x: 0, y: 0, width: 25, height: 17)
    let barButton = UIBarButtonItem.init(customView: button)
    self.navigationItem.rightBarButtonItem = barButton

答案 3 :(得分:-1)

请参阅以下代码。

let button: UIButton = UIButton (type: UIButtonType.Custom)
button.setImage(UIImage(named: "emojiImage"), forState: UIControlState.Normal)
//button.addTarget(self, action: "emojiImageButtonPressed:", forControlEvents: UIControlEvents.TouchUpInside)
button.frame = CGRectMake(0, 0, 30, 30)
let barButton = UIBarButtonItem(customView: button)

self.navigationItem.rightBarButtonItem = barButton

确保您的图片必须是普通(透明)背景。

如果您想要点击活动,请取消注释button.addTarget(self, action: "emojiImageButtonPressed:", forControlEvents: UIControlEvents.TouchUpInside)并将以下功能调用。

func emojiImageButtonPressed(btn : UIButton) {

    print("emojiImageButtonPressed")
}