Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set circle button with background image on right navigation bar item

I have tried to put circle button on right navigation bar of iOS but unfortunately When I use button background it doesn't round the image it shows square shape background image but When I remove image and put background colour it round the button with background colour. Code that 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
like image 588
Andrew Hall Avatar asked Mar 02 '16 10:03

Andrew Hall


2 Answers

Try to use this code.. for rounded button with 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
like image 50
mohit_IBS Avatar answered Sep 30 '22 14:09

mohit_IBS


I made a solution for Swift 4, you need to resize the image and the 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 you need:

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
    }
}
like image 28
K. Janjuha Avatar answered Sep 30 '22 16:09

K. Janjuha