Search code examples
iosswiftxcodeios11xcode9

Weird NavBar stretching in ios 11 Xcode 9


Having a little problem with my NavBar buttons atm. I upgraded to Xcode 9 / ios 11 and suddenly the UIBarButtonItems which used to be little buttons on the side of navbar (like your back or edit buttons in standard ios apps) have begun stretching massively. Here is a photo:

The Problem

My code is simply:

self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: image2, style: .plain, target: self, action: #selector(messageScreen))

Does anyone know how to fix this? Thank you.


Solution

  • Swift:

    let widthConstraint = button.widthAnchor.constraint(equalToConstant: 30)
    let heightConstraint = button.heightAnchor.constraint(equalToConstant: 30)
    heightConstraint.isActive = true
    widthConstraint.isActive = true
    

    Objective C:

    [button.widthAnchor constraintEqualToConstant:30].active = YES;
    [button.heightAnchor constraintEqualToConstant:30].active = YES;
    

    // add those lines before setting navigation buttons