UINavigationBar titleView higher than the plan itself

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

        // 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
    }
}

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;
+4
2

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;
}

iPhone 6 image

+2

UINavigationBar 65. 10 yOffset Title for StatusBar. , U 10 65 yOffset of NavigationBar. : (65-10) = 55.

, u :

  • , ,

    let buttonWrapper = UIView(frame: CGRect(x: 0, y: 0, width: 45.0, height: 45.0));

  • yOffset , ,

    let buttonWrapper = UIView(frame: CGRect(x: 0, y: -8, width: 60.0, height: 60.0));

+1

Source: https://habr.com/ru/post/1583880/


All Articles