gpt4 book ai didi

ios - Xcode 7.3.1 : Set background image to UINavigationBar and display back button

转载 作者:行者123 更新时间:2023-11-28 10:18:04 25 4
gpt4 key购买 nike

我想将应用程序的 Logo 设置为 UINavigationBar 的背景图片,当用户遍历应用程序时,它应该在其顶部显示 Logo 和后退按钮。

下面是我用过的代码:

 func setNavigationBar() {
let navigationBarHeight: CGFloat = self.navigationController!.navigationBar.frame.height
let screenSize: CGRect = UIScreen.mainScreen().bounds

let objCustomView = CustomView(frame: CGRect(x: 0, y: 0, width: screenSize.width, height: navigationBarHeight))

let objWindow = UIApplication.sharedApplication().keyWindow
objWindow?.addSubview(objCustomView)

self.navigationItem.setHidesBackButton(false, animated:true);
self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"", style:.Plain, target:nil, action:nil)
}

问题在于后退按钮位于图像后面。

Screenshot 1

如何解决这个问题?

在引用了@NDoc 的帖子后,我在左边得到了额外的空间。为什么这样?另外,后退按钮应该是白色的,没有返回文字,即只有<箭头。

下面是自定义 View 的代码:

class CustomView: UIView {      
var imgLogo = UIImageView(frame:CGRectZero)

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

let screenSize: CGRect = UIScreen.mainScreen().bounds
imgLogo.frame = CGRectMake(0, 0, screenSize.width, 44.0)

setup()
}

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

setup()
}

func setup() {
imgLogo.image = UIImage(named:"BoM_Logo")
self.addSubview(imgLogo)
}
}

最佳答案

您可以在 leftBarButtonItem 中显示您的 Logo 并将 leftItemsSupplementBackButton 设置为 true 以显示 backButton 也像这样.

let logoView = UIImageView(frame: CGRect(x: 0, y: 0, width: 60, height: 30))
logoView.image = UIImage(named: "Logo")
let item = UIBarButtonItem(customView: logoView)
self.navigationItem.leftBarButtonItem = item

要显示带有 Logo 图像的后退按钮,请将 leftItemsSupplementBackButton 设置为 true

self.navigationItem.leftItemsSupplementBackButton = true

编辑:

如果您想要自定义箭头,那么您需要使用 leftBarButtonItems 并传递 BarButtonItem 数组,无需将 leftItemsSupplementBackButton 设置为 像这样。

let logoView = UIImageView(frame: CGRect(x: 0, y: 0, width: 60, height: 30))
logoView.image = UIImage(named: "Logo")
let logoItem = UIBarButtonItem(customView: logoView)
let btnBack = UIButton(frame: CGRect(x: 0, y: 0, width: 25, height: 25))
btnBack.setImage(UIImage(named: "Back_Arrow"), forState: .Normal)
btnBack.addTarget(self, action: #selector(self.buttonAction(_:)), forControlEvents: .TouchUpInside)
let backItem = UIBarButtonItem(customView: btnBack)
self.navigationItem.leftBarButtonItems = [backItem, logoItem]

注意:不要忘记在您的 viewController 中添加 buttonAction 操作方法。

关于ios - Xcode 7.3.1 : Set background image to UINavigationBar and display back button,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39099857/

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