1

I found a post with similar a question but they weren't answered properly, or weren't answered at all. This piece of code was working fine in Swift 2.2 but now all the sudden it is not working in Swift 4. The UIBarButtonItem image is being stretched, completely blocking the title.

let imageData = (trans.value(forKey: "profilePicture")) as? Data
storedImage = UIImage.init(data: imageData!)
let button = UIButton.init(type: .custom)
button.setImage((storedImage), for: UIControlState.normal)
button.addTarget(self, action: #selector(ParentViewController.buttonPressed), for: UIControlEvents.touchUpInside)
button.frame = CGRect(x: 0, y: 0, width: 25, height: 25)
button.layer.cornerRadius = 0.5 * button.bounds.size.width
button.clipsToBounds = true
button.layer.borderWidth = 1
//button.layer.borderColor = UIColor(red: 178/255, green: 0/255, blue: 0/255, alpha: 1).cgColor
button.layer.borderColor = UIColor.white.cgColor
let barButton = UIBarButtonItem(customView: button)
self.navigationItem.leftBarButtonItem = barButton

enter image description here

rmaddy
  • 314,917
  • 42
  • 532
  • 579
tyguy
  • 358
  • 2
  • 4
  • 16
  • Seems something similar to [this issue](https://stackoverflow.com/q/46247451/6541007). It's not the problem of Swift 4 but the problem of iOS 11. (You can try with other version of iOS simulators.) – OOPer Jul 10 '18 at 19:52
  • Why use `UIButton`? Why not create the `UIBarButtonItem` directly with the image? – rmaddy Jul 10 '18 at 20:38
  • Because I want to use colored images for the button and the `UIBarButtonItem` won't let you do that, so we create a `UIButton` assign the image to that and add the `UIButton` as a custom button to the `UIBarButton`. – tyguy Jul 10 '18 at 21:20
  • You can get the image to appear correctly. See https://stackoverflow.com/questions/34101715/set-custom-images-to-the-uibarbuttonitem-but-it-doesnt-show-any-image – rmaddy Jul 11 '18 at 01:19
  • Please Refer this Answer. **Link** :- https://stackoverflow.com/a/50961515/4971043 – Sahdevsinh Chavda Jul 11 '18 at 11:50

0 Answers0