增加NavigationBar高度 [英] Increase NavigationBar height
问题描述
我有以下代码:
func navbarbutton() {
UIView.animateWithDuration(0.2, animations: { () -> Void in
let current = self.navigationController?.navigationBar.frame
self.navigationController?.navigationBar.frame = CGRectMake(self.frame!.origin.x, self.frame!.origin.y, self.frame!.size.width, current!.size.height + 50)
self.navigationController?.navigationBar.layoutIfNeeded()
})
}
我能够将导航栏的高度增加50 dp。这对我来说不是问题。我遇到的问题是 UIBarButtonItems
都与底部对齐。如何让它们与顶部对齐,以便我可以自己添加更多底部?我按照图像得到了一些东西:
I'm able to increase the height of the navigation bar by 50 dp. That's not the issue for me. The issue I'm having is that the UIBarButtonItems
are all aligned to the bottom. How can I get them aligned to the top so that I can add more to the bottom myself? I'm getting something as per the image:
是否可以将它与顶部对齐?
Is it possible to get it aligned to the top?
推荐答案
试用此代码:
注意:代码在Swift 3中测试。
Note: Code tested in Swift 3.
答案1: 更新回答
class ViewController: UIViewController {
var customBar: UINavigationBar = UINavigationBar()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//Title
title = "Some Title"
// Add bar button item
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(addTapped))
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(addTapped))
self.customBar.frame = CGRect(x:0, y:0, width:view.frame.width, height:(navigationController?.navigationBar.frame.height)! + 50)
self.customBar.backgroundColor = UIColor.green
self.view.addSubview(customBar)
}
func addTapped() {
print("Button Pressed")
}
输出:
答案2:
override var isViewLoaded: Bool {
// Add bar button item
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(addTapped))
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(addTapped))
//Vertical and Horizonal barButtonItem position offset
navigationItem.leftBarButtonItem?.setTitlePositionAdjustment(UIOffset(horizontal: 0, vertical: 20), for: UIBarMetrics.default)
navigationItem.rightBarButtonItem?.setTitlePositionAdjustment(UIOffset(horizontal: 0, vertical: 20), for: UIBarMetrics.default)
return true
}
func addTapped() {
print("Button Pressed")
}
注意:上面的代码只适用于 isViewLoaded:Bool 方法。但是,没有运气。当我在其他viewLoad方法中尝试此代码时。
Note: Above code only works in isViewLoaded: Bool method.But, No luck.When, I tried this code in other viewLoad method.
输出1: barButtonItem垂直向上移动了20个像素。
Output 1: barButtonItem moved 20 pixel up vertically.
输出2: barButtonItem垂直向下移动20个像素。
Output 2: barButtonItem moved 20 pixel down vertically.
希望,以上代码可以解决您的问题。
Hope, Above code fix your problem.
这篇关于增加NavigationBar高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!