So I'm trying to make a center button on a UINavigationBar similar to the popular UITabBar setting. But he had a little problem.
In principle, everything works well, I have a bar size setting showing the titleView correctly when working with AutoLayout (topLayoutGuide). Even push animation works well. But pop animation fails. These are screenshots or cropping to borders, and I cannot find a good job that does not make me just want to create a custom control and skip the UINavigationBar.
Here is a video showing what is happening. http://tinypic.com/r/2vl8yl1/8
Here is the code for custom navigationBar:
private let NavigationBarOffset: CGFloat = 10.0
class ActionNavigationBar: UINavigationBar {
override init(frame: CGRect) {
super.init(frame: frame)
self.clipsToBounds = false
self.contentMode = .Redraw
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)
expectedSize.height += NavigationBarOffset
return expectedSize
}
}
Here is the code that enters the button there:
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;