How can I make a button have a rounded border in Swift?

后端 未结 15 2107
庸人自扰
庸人自扰 2021-01-29 17:50

I\'m building an app using swift in the latest version of Xcode 6, and would like to know how I can modify my button so that it can have a rounded border that I could adjust mys

15条回答
  •  说谎
    说谎 (楼主)
    2021-01-29 18:16

    This class is based from all the comments and suggestions in answers, and also can be designable directly from xcode. Copy to your project and insert any UIButton and change to use custom class, now just select border or background color from xcode for normal and/or highlighted states.

    //
    //  RoundedButton.swift
    //
    
    import UIKit
    
    @IBDesignable
    class RoundedButton:UIButton {
    
        @IBInspectable var borderWidth: CGFloat = 0 {
            didSet {
                layer.borderWidth = borderWidth
            }
        }
        //Normal state bg and border
        @IBInspectable var normalBorderColor: UIColor? {
            didSet {
                layer.borderColor = normalBorderColor?.CGColor
            }
        }
    
        @IBInspectable var normalBackgroundColor: UIColor? {
            didSet {
                setBgColorForState(normalBackgroundColor, forState: .Normal)
            }
        }
    
    
        //Highlighted state bg and border
        @IBInspectable var highlightedBorderColor: UIColor?
    
        @IBInspectable var highlightedBackgroundColor: UIColor? {
            didSet {
                setBgColorForState(highlightedBackgroundColor, forState: .Highlighted)
            }
        }
    
    
        private func setBgColorForState(color: UIColor?, forState: UIControlState){
            if color != nil {
                setBackgroundImage(UIImage.imageWithColor(color!), forState: forState)
    
            } else {
                setBackgroundImage(nil, forState: forState)
            }
        }
    
        override func layoutSubviews() {
            super.layoutSubviews()
    
            layer.cornerRadius = layer.frame.height / 2
            clipsToBounds = true
    
            if borderWidth > 0 {
                if state == .Normal && !CGColorEqualToColor(layer.borderColor, normalBorderColor?.CGColor) {
                    layer.borderColor = normalBorderColor?.CGColor
                } else if state == .Highlighted && highlightedBorderColor != nil{
                    layer.borderColor = highlightedBorderColor!.CGColor
                }
            }
        }
    
    }
    
    //Extension Required by RoundedButton to create UIImage from UIColor
    extension UIImage {
        class func imageWithColor(color: UIColor) -> UIImage {
            let rect: CGRect = CGRectMake(0, 0, 1, 1)
            UIGraphicsBeginImageContextWithOptions(CGSizeMake(1, 1), false, 1.0)
            color.setFill()
            UIRectFill(rect)
            let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return image
        }
    }
    

提交回复
热议问题