How to add only a TOP border on a UIButton?

后端 未结 11 1678
自闭症患者
自闭症患者 2020-12-13 03:45

I know how to add border to a button in iOS 7, with the following code :

[[myButton layer] setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5]          


        
11条回答
  •  囚心锁ツ
    2020-12-13 04:48

    In Swift add an extension for UIView class like this:

    **Swift 3 ***

    extension UIView {
        func addTopBorderWithColor(color: UIColor, width: CGFloat) {
            let border = CALayer()
            border.backgroundColor = color.cgColor
            border.frame = CGRect(x:0,y: 0, width:self.frame.size.width, height:width)
            self.layer.addSublayer(border)
        }
    
        func addRightBorderWithColor(color: UIColor, width: CGFloat) {
            let border = CALayer()
            border.backgroundColor = color.cgColor
            border.frame = CGRect(x: self.frame.size.width - width,y: 0, width:width, height:self.frame.size.height)
            self.layer.addSublayer(border)
        }
    
        func addBottomBorderWithColor(color: UIColor, width: CGFloat) {
            let border = CALayer()
            border.backgroundColor = color.cgColor
            border.frame = CGRect(x:0, y:self.frame.size.height - width, width:self.frame.size.width, height:width)
            self.layer.addSublayer(border)
        }
    
        func addLeftBorderWithColor(color: UIColor, width: CGFloat) {
            let border = CALayer()
            border.backgroundColor = color.cgColor
            border.frame = CGRect(x:0, y:0, width:width, height:self.frame.size.height)
            self.layer.addSublayer(border)
        }
    }
    

    I got this extension from this link:UIView bottom border?

    Then call the function like this

    var innerView : UIView?
    let borderWidth: CGFloat = 1.0
    let borderColor : UIColor =  UIColor.redColor()
    innerView!.addTopBorderWithColor(borderColor, width: borderWidth)
    

    For adaptive layout use this one

    Swift 3

    extension UIView {
    
        func addTopBorder(_ color: UIColor, height: CGFloat) {
            let border = UIView()
            border.backgroundColor = color
            border.translatesAutoresizingMaskIntoConstraints = false
            self.addSubview(border)
            border.addConstraint(NSLayoutConstraint(item: border,
                attribute: NSLayoutAttribute.height,
                relatedBy: NSLayoutRelation.equal,
                toItem: nil,
                attribute: NSLayoutAttribute.height,
                multiplier: 1, constant: height))
            self.addConstraint(NSLayoutConstraint(item: border,
                attribute: NSLayoutAttribute.top,
                relatedBy: NSLayoutRelation.equal,
                toItem: self,
                attribute: NSLayoutAttribute.top,
                multiplier: 1, constant: 0))
            self.addConstraint(NSLayoutConstraint(item: border,
                attribute: NSLayoutAttribute.leading,
                relatedBy: NSLayoutRelation.equal,
                toItem: self,
                attribute: NSLayoutAttribute.leading,
                multiplier: 1, constant: 0))
            self.addConstraint(NSLayoutConstraint(item: border,
                attribute: NSLayoutAttribute.trailing,
                relatedBy: NSLayoutRelation.equal,
                toItem: self,
                attribute: NSLayoutAttribute.trailing,
                multiplier: 1, constant: 0))
        }
    
        func addBottomBorder(_ color: UIColor, height: CGFloat) {
            let border = UIView()
            border.backgroundColor = color
            border.translatesAutoresizingMaskIntoConstraints = false
            self.addSubview(border)
            border.addConstraint(NSLayoutConstraint(item: border,
                attribute: NSLayoutAttribute.height,
                relatedBy: NSLayoutRelation.equal,
                toItem: nil,
                attribute: NSLayoutAttribute.height,
                multiplier: 1, constant: height))
            self.addConstraint(NSLayoutConstraint(item: border,
                attribute: NSLayoutAttribute.bottom,
                relatedBy: NSLayoutRelation.equal,
                toItem: self,
                attribute: NSLayoutAttribute.bottom,
                multiplier: 1, constant: 0))
            self.addConstraint(NSLayoutConstraint(item: border,
                attribute: NSLayoutAttribute.leading,
                relatedBy: NSLayoutRelation.equal,
                toItem: self,
                attribute: NSLayoutAttribute.leading,
                multiplier: 1, constant: 0))
            self.addConstraint(NSLayoutConstraint(item: border,
                attribute: NSLayoutAttribute.trailing,
                relatedBy: NSLayoutRelation.equal,
                toItem: self,
                attribute: NSLayoutAttribute.trailing,
                multiplier: 1, constant: 0))
        }
    
        func addLeftBorder(_ color: UIColor, width: CGFloat) {
            let border = UIView()
            border.backgroundColor = color
            border.translatesAutoresizingMaskIntoConstraints = false
            self.addSubview(border)
            border.addConstraint(NSLayoutConstraint(item: border,
                attribute: NSLayoutAttribute.width,
                relatedBy: NSLayoutRelation.equal,
                toItem: nil,
                attribute: NSLayoutAttribute.width,
                multiplier: 1, constant: width))
            self.addConstraint(NSLayoutConstraint(item: border,
                attribute: NSLayoutAttribute.leading,
                relatedBy: NSLayoutRelation.equal,
                toItem: self,
                attribute: NSLayoutAttribute.leading,
                multiplier: 1, constant: 0))
            self.addConstraint(NSLayoutConstraint(item: border,
                attribute: NSLayoutAttribute.bottom,
                relatedBy: NSLayoutRelation.equal,
                toItem: self,
                attribute: NSLayoutAttribute.bottom,
                multiplier: 1, constant: 0))
            self.addConstraint(NSLayoutConstraint(item: border,
                attribute: NSLayoutAttribute.top,
                relatedBy: NSLayoutRelation.equal,
                toItem: self,
                attribute: NSLayoutAttribute.top,
                multiplier: 1, constant: 0))
        }
    
        func addRightBorder(_ color: UIColor, width: CGFloat) {
            let border = UIView()
            border.backgroundColor = color
            border.translatesAutoresizingMaskIntoConstraints = false
            self.addSubview(border)
            border.addConstraint(NSLayoutConstraint(item: border,
                attribute: NSLayoutAttribute.width,
                relatedBy: NSLayoutRelation.equal,
                toItem: nil,
                attribute: NSLayoutAttribute.width,
                multiplier: 1, constant: width))
            self.addConstraint(NSLayoutConstraint(item: border,
                attribute: NSLayoutAttribute.trailing,
                relatedBy: NSLayoutRelation.equal,
                toItem: self,
                attribute: NSLayoutAttribute.trailing,
                multiplier: 1, constant: 0))
            self.addConstraint(NSLayoutConstraint(item: border,
                attribute: NSLayoutAttribute.bottom,
                relatedBy: NSLayoutRelation.equal,
                toItem: self,
                attribute: NSLayoutAttribute.bottom,
                multiplier: 1, constant: 0))
            self.addConstraint(NSLayoutConstraint(item: border,
                attribute: NSLayoutAttribute.top,
                relatedBy: NSLayoutRelation.equal,
                toItem: self,
                attribute: NSLayoutAttribute.top,
                multiplier: 1, constant: 0))
        }
    }
    

    Usage:

    button!.addTopBorder(UIColor(red: 247.0/255.0, green: 147.0/255.0, blue: 29.0/255.0, alpha: 0.5), height: borderWidth)
    

提交回复
热议问题