所以我尝试在 UINavigationBar 上做一个中心按钮,类似于流行的 UITabBar 自定义。但是它有一个小问题。
基本上一切正常,我调整了栏的大小,正确显示了 titleView,使用了 AutoLayout (topLayoutGuide)。甚至推送动画也运行良好。但是流行动画失败了。它是屏幕截图或剪辑到边界,我找不到一个好的解决方法,这不会让我只想制作一个自定义控件并跳过 UINavigationBar。
这是一段视频,显示了具体情况。 http://tinypic.com/r/2vl8yl1/8
下面是自定义导航栏的一些代码:
private let NavigationBarOffset: CGFloat = 10.0
class ActionNavigationBar: UINavigationBar {
override init(frame: CGRect) {
super.init(frame: frame)
self.clipsToBounds = false
self.contentMode = .Redraw
// Slide the original navigationBar up to make room for the actionbutton
transform = CGAffineTransformMakeTranslation(0.0, -NavigationBarOffset)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func sizeThatFits(size: CGSize) -> CGSize {
var expectedSize = super.sizeThatFits(size)
// Adjust the height of the navigation bar
expectedSize.height += NavigationBarOffset
return expectedSize
}
}
这是获取按钮的代码:
navigationItem.title = "Browse"
navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Bookmarks, target: nil, action: nil)
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Search, target: self, action: "next")
let button = UIButton()
button.setTranslatesAutoresizingMaskIntoConstraints(false)
button.setTitle("+", forState: .Normal)
button.setTitle("", forState: .Highlighted)
button.titleLabel?.font = UIFont.boldSystemFontOfSize(40.0)
button.backgroundColor = self.view.tintColor
button.layer.cornerRadius = 27.0
let buttonWrapper = UIView(frame: CGRect(x: 0, y: 0, width: 60.0, height: 60.0));
buttonWrapper.addSubview(button)
NSLayoutConstraint(item: button, attribute: .CenterX, relatedBy: .Equal, toItem: buttonWrapper, attribute: .CenterX, multiplier: 1.0, constant: 0.0).active = true
NSLayoutConstraint(item: button, attribute: .Bottom, relatedBy: .Equal, toItem: buttonWrapper, attribute: .Bottom, multiplier: 1.0, constant: 0.0).active = true
NSLayoutConstraint(item: button, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 54.0).active = true
NSLayoutConstraint(item: button, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 54.0).active = true
navigationItem.titleView = buttonWrapper;
最佳答案
我尝试在 override func viewDidLoad()
中进行以下操作。不使用自定义导航栏对我来说工作正常。
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//self.edgesForExtendedLayout = UIRectEdge.None
//navigationItem.title = "Browse"
navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Bookmarks, target: nil, action: nil)
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Search, target: self, action: "next")
let button = UIButton()
//button.setTranslatesAutoresizingMaskIntoConstraints(false)
button.frame = CGRectMake(0, 0, 40, 40)
button.setTitle("+", forState: .Normal)
button.setTitle("", forState: .Highlighted)
button.titleLabel?.font = UIFont.boldSystemFontOfSize(40.0)
button.backgroundColor = self.view.tintColor
button.layer.cornerRadius = 20
let buttonWrapper = UIView(frame: CGRect( 0, y: 0, width: 40, height: 40));
//buttonWrapper.backgroundColor = UIColor.redColor()
buttonWrapper.addSubview(button)
/*NSLayoutConstraint(item: button, attribute: .CenterX, relatedBy: .Equal, toItem: buttonWrapper, attribute: .CenterX, multiplier: 1.0, constant: 0.0).active = true
NSLayoutConstraint(item: button, attribute: .Bottom, relatedBy: .Equal, toItem: buttonWrapper, attribute: .Bottom, multiplier: 1.0, constant: 0.0).active = true
NSLayoutConstraint(item: button, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 54.0).active = true
NSLayoutConstraint(item: button, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 54.0).active = true*/
navigationItem.titleView = buttonWrapper;
}
关于ios - UINavigationBar titleView 比栏本高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29784798/