Error on Adding Uinavigationbar Button with Image

Error on adding UINavigationbar button with image

Custom Left-bar button image :

    let btnBack = UIButton()
btnBack.setImage(#imageLiteral(resourceName: "back"), for: .normal)
btnBack.frame = CGRect.init(x: 0, y: 0, width: 30, height: 30)
btnBack.addTarget(self, action: #selector(addTapped), for: .touchUpInside)
btnBack.imageView?.contentMode = .scaleAspectFit
let leftBack = UIBarButtonItem.init(customView: btnBack)

navigationItem.leftBarButtonItem = leftBack

Also check the size of image which you are trying to set for left-bar
button.
I used 35 x 35 for 2x image and 53 x 53 size for 3x image.

navigation bar rightbaritem image-button bug iOS 11

Reason

The problem appears because from ios 11 UIBarButtonItem uses autolayout instead of dealing with frames.

Solution

You should add width constraint for this image-button if you use Xcode 9.

 button.widthAnchor.constraint(equalToConstant: 32.0).isActive = true
button.heightAnchor.constraint(equalToConstant: 32.0).isActive = true

PS

button is not UIBarButtonItem, it is UIButton inside UIBarButtonItem. You should set constraint not for UIBarButtonItem, but for elements inside it.

Image in navigation bar button not rendering after updating to Xcode 9

In iOS 11 you need to add/set constraints of height and with of UIButton

For notificationBtn

let widthConstraint = notificationBtn.widthAnchor.constraint(equalToConstant: 35)
let heightConstraint = notificationBtn.heightAnchor.constraint(equalToConstant: 35)
heightConstraint.isActive = true
widthConstraint.isActive = true

Apply same for profileBtn too.

swift: setting back button image in nav bar

I have figured out by looking into sample code.
1) Create a bar button item in storyboard.
2) Link that button to controller using IBOutlet
3) Add image to the button

 var backImg: UIImage = UIImage(named: "back_btn")
backBtn.setBackgroundImage(backImg, forState: .Normal, barMetrics: .Default)

PS: image should be added to Images.xcassets folder, see sample code, UICatalog , for details.

image for nav bar button item swift

Try This

let button = UIButton(type: UIButtonType.Custom)
button.setImage(UIImage(named: "yourImageName.png"), forState: UIControlState.Normal)
button.addTarget(self, action:Selector("callMethod"), forControlEvents: UIControlEvents.TouchDragInside)
button.frame=CGRectMake(0, 0, 30, 30)
let barButton = UIBarButtonItem(customView: button)
self.navigationItem.leftBarButtonItems = [newBackButton,barButton]

For Swift 3

let button = UIButton.init(type: .custom)
button.setImage(UIImage.init(named: "yourImageName.png"), for: UIControlState.normal)
button.addTarget(self, action:#selector(ViewController.callMethod), for:.touchUpInside)
button.frame = CGRect.init(x: 0, y: 0, width: 30, height: 30) //CGRectMake(0, 0, 30, 30)
let barButton = UIBarButtonItem.init(customView: button)
self.navigationItem.leftBarButtonItem = barButton

Swift 4

let button = UIButton(type: UIButton.ButtonType.custom)
button.setImage(UIImage(named: "getstarted"), for: .normal)
button.addTarget(self, action:#selector(callMethod), for: .touchDragInside)
button.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
let barButton = UIBarButtonItem(customView: button)
self.navigationItem.leftBarButtonItems = [barButton]

Here is action

@objc func callMethod() {   
//do stuff here
}

Navigation bar button item image color is different when design through xib of xcode5

First, I agree with @Desdenova's comment.

The two images do not look the same, one has hard right angle edges for each line, and the other rounded.

Make sure you are using the correct image file.

If this is the case, awesome, problem solved without deviating from your xib implementation. If not, just do it programmatically (as per @shankars code).

But another thing to note, I've run into problems setting custom image files to buttons, where the image gets tweaked... make sure to use UIImageRenderingModeAlwaysOriginal when setting the image to the button:

Objective-C:

[button setImage:[[UIImage imageNamed:@"imageName.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];

Swift:

someBarButtonItem.image = UIImage(named: "yourPictureName")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)

Swift 3:

someBarButtonItem.image = UIImage(named:"myImage")?.withRenderingMode(.alwaysOriginal)


Related Topics



Leave a reply



Submit