How to set a circle button with a background image in the panel of the right navigation element

I tried to put the circle button in the right navigation bar of iOS, but unfortunately when I use the background element of the button, it is not around the image, it shows the image in square form, but when I delete the image and overlay the background color around the button with the background color. The code I tried:

        let button = UIButton()
        button.frame = CGRectMake(0, 0, 40, 40)
        button.layer.cornerRadius = 0.5 * button.bounds.size.width
        button.setImage(self.myPic, forState: .Normal)            
        let barButton = UIBarButtonItem()
        barButton.customView = button
        self.navigationItem.rightBarButtonItem = barButton
+4
source share
3 answers

Try using this code. for a rounded button with an image -

let button = UIButton()
button.frame = CGRectMake(0, 0, 40, 40)
let color = UIColor(patternImage: UIImage(named: "btnImage")!)
button.backgroundColor = color
button.layer.cornerRadius = 0.5 * button.bounds.size.width
let barButton = UIBarButtonItem()
barButton.customView = button
self.navigationItem.rightBarButtonItem = barButton

With actual image ---

   let button = UIButton()
    button.frame = CGRectMake(0, 0, 40, 40)

    let image = UIImage(named: "btnImage")!

    UIGraphicsBeginImageContextWithOptions(button.frame.size, false, image.scale)
    let rect  = CGRectMake(0, 0, button.frame.size.width, button.frame.size.height)
    UIBezierPath(roundedRect: rect, cornerRadius: rect.width/2).addClip()
    image.drawInRect(rect)

    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()


    let color = UIColor(patternImage: newImage)
    button.backgroundColor = color
    button.layer.cornerRadius = 0.5 * button.bounds.size.width
    let barButton = UIBarButtonItem()
    barButton.customView = button
    self.navigationItem.rightBarButtonItem = barButton
+9
source

quick 2 set bar button in circle

func setProfileImageOnBarButton() {

    let button = UIButton()
    button.setImage(profileImage, forState: UIControlState.Normal)
    button.addTarget(self, action:#selector(self.openUserProfile), forControlEvents: UIControlEvents.TouchUpInside)
    button.frame = CGRectMake(0, 0, 36, 36)
    button.layer.cornerRadius = CGRectGetWidth(button.frame) / 2
    button.layer.masksToBounds = true
    let barButton = UIBarButtonItem(customView: button)
    self.navigationItem.rightBarButtonItem = barButton

}
+1
source

I made a solution for Swift 4, you need to resize the image and frame too

let avatarSize: CGFloat = 30
let button = UIButton()
button.frame = CGRect(x: 0, y: 0, width: avatarSize, height: avatarSize)
button.setImage(UIImage(named: "avatar")?.resizeImage(avatarSize, opaque: false), for: .normal)

if let buttonImageView = button.imageView {
    button.imageView?.layer.cornerRadius = buttonImageView.frame.size.width / 2
    button.imageView?.clipsToBounds = true
    button.imageView?.contentMode = .scaleAspectFit
}

Extension needed:

extension UIImage {
    func resizeImage(_ dimension: CGFloat, opaque: Bool, contentMode: 
        UIViewContentMode = .scaleAspectFit) -> UIImage {
        var width: CGFloat
        var height: CGFloat
        var newImage: UIImage

        let size = self.size
        let aspectRatio =  size.width/size.height

        switch contentMode {
        case .scaleAspectFit:
            if aspectRatio > 1 {                            // Landscape image
                width = dimension
                height = dimension / aspectRatio
            } else {                                        // Portrait image
                height = dimension
                width = dimension * aspectRatio
            }

        default:
            fatalError("UIIMage.resizeToFit(): FATAL: Unimplemented ContentMode")
        }

        if #available(iOS 10.0, *) {
            let renderFormat = UIGraphicsImageRendererFormat.default()
            renderFormat.opaque = opaque
            let renderer = UIGraphicsImageRenderer(size: CGSize(width: width, height: height), format: renderFormat)
            newImage = renderer.image {
                (context) in
                self.draw(in: CGRect(x: 0, y: 0, width: width, height: height))
            }
        } else {
            UIGraphicsBeginImageContextWithOptions(CGSize(width: width, height: height), opaque, 0)
            self.draw(in: CGRect(x: 0, y: 0, width: width, height: height))
            newImage = UIGraphicsGetImageFromCurrentImageContext()!
            UIGraphicsEndImageContext()
        }

        return newImage
    }
}
0
source

All Articles