Xcode 7.3.1:将背景图像设置为 UINavigationBar 并显示返回按钮
Posted
技术标签:
【中文标题】Xcode 7.3.1:将背景图像设置为 UINavigationBar 并显示返回按钮【英文标题】:Xcode 7.3.1 : Set background image to UINavigationBar and display back button 【发布时间】:2016-08-23 11:25:11 【问题描述】:我想将应用程序的标志设置为UINavigationBar
的背景图片,当用户进入应用程序时,它应该在其顶部显示标志以及后退按钮。
下面是我用过的代码:
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)
问题在于后退按钮位于图像后面。
如何解决这个问题?
在参考了 @NDoc 的帖子后,我在左侧获得了额外的空间。为什么这样? 此外,后退按钮应该是白色的,没有后退文字,即只有
下面是 customView 的代码:
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)
【问题讨论】:
【参考方案1】:您可以在leftBarButtonItem
中显示您的徽标并将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
显示带有您的徽标图像的后退按钮,将leftItemsSupplementBackButton
设置为true
self.navigationItem.leftItemsSupplementBackButton = true
编辑:
如果您想要自定义箭头,那么您需要使用leftBarButtonItems
并传递BarButtonItem
的数组,而无需像这样将leftItemsSupplementBackButton
设置为true
。
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
操作方法。
【讨论】:
请检查问题。使用此代码我还有另一个问题【参考方案2】:在你的 appDelegate 中试试这个
let image = UIImage.init(named:"upper-bar.png")
UINavigationBar.appearance().setBackgroundImage(image,forBarMetrics:UIBarMetrics.Default)
对于后退按钮,请在 viewController 的 viewDidLoad() 中尝试此操作
let image1 = UIImage(named: "go10.png") as UIImage?
let btnLeft = UIButton(type: .Custom)
btnLeft.frame = CGRectMake(0, 0, 25, 25)
btnLeft.setImage(image1,forState:UIControlState.Normal)
btnLeft.addTarget(self, action:(#selector(NameofyourViewController.backBtn(_:))),forControlEvents:UIControlEvents.TouchUpInside)
let leftBarButton = UIBarButtonItem(customView: btnLeft)
self.navigationItem.leftBarButtonItem = leftBarButton
@IBAction func backBtn(sender: UIButton)
self.navigationController?.popViewControllerAnimated(true)
【讨论】:
以上是关于Xcode 7.3.1:将背景图像设置为 UINavigationBar 并显示返回按钮的主要内容,如果未能解决你的问题,请参考以下文章
在 Interface Builder (XCode 4) 中设置 UIButton 拉伸背景图像