Swift Cannot Round the Corners of my UIButton

假装没事ソ 提交于 2019-12-30 13:26:58

问题


I've looked at almost every stackoverflow solution and for some odd reason my button will not round the corners. Can someone check and see what im doing wrong?

let goToMapsButton = UIButton(type: .custom)
    scrollView.addSubview(goToMapsButton)
       _ = goToMapsButton.anchor(map.bottomAnchor, left: nil, bottom: seperator.topAnchor, right: self.view.rightAnchor, topConstant: 16, leftConstant: 0, bottomConstant: 16, rightConstant: 16, widthConstant: 50, heightConstant: 50)


    goToMapsButton.backgroundColor = .green
    goToMapsButton.layer.cornerRadius = 0.5 * goToMapsButton.bounds.size.width
    goToMapsButton.clipsToBounds = true
    goToMapsButton.layer.masksToBounds = true

Btw, Im doing this all in the viewDidLoad section of the view controller, if that info makes a difference.

Here is the full viewDidLoadClass for reference:` override func viewDidLoad() { super.viewDidLoad()

    let map = MKMapView()
    let view1 = UIView()
    view1.backgroundColor = .red

    let storeAddress = UILabel()
    storeAddress.text = "318 Atwood Avenue"
    storeAddress.font = UIFont.systemFont(ofSize: 20, weight: UIFont.Weight.medium)

    let storeCity = UILabel()
    storeCity.text = "Rainy River"
    storeCity.font = UIFont.systemFont(ofSize: 20, weight: UIFont.Weight.medium)

    let seperator = UIView()
    seperator.backgroundColor = .lightGray

    let goToMapsButton = UIButton(type: .custom)


    scrollView.addSubview(map)
    scrollView.addSubview(view1)
    scrollView.addSubview(storeAddress)
    scrollView.addSubview(storeCity)
    scrollView.addSubview(goToMapsButton)
    scrollView.addSubview(seperator)

    map.anchorToTop(scrollView.topAnchor, left: self.view.leftAnchor, bottom: nil, right: self.view.rightAnchor)
    map.heightAnchor.constraint(equalTo: self.view.heightAnchor, multiplier: 0.6).isActive = true

    _ = storeAddress.anchor(map.bottomAnchor, left: self.view.leftAnchor, bottom: nil, right: nil, topConstant: 16, leftConstant: 16, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0)
    _ = storeCity.anchor(storeAddress.bottomAnchor, left: self.view.leftAnchor, bottom: nil, right: nil, topConstant: 8, leftConstant: 16, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0)

    _ = goToMapsButton.anchor(map.bottomAnchor, left: nil, bottom: nil, right: self.view.rightAnchor, topConstant: 16, leftConstant: 0, bottomConstant: 16, rightConstant: 16, widthConstant: 50, heightConstant: 50)
    goToMapsButton.backgroundColor = .green
    print(goToMapsButton.frame.width)
    goToMapsButton.layer.cornerRadius = 0.25 * goToMapsButton.frame.width
    goToMapsButton.clipsToBounds = true
    goToMapsButton.layer.masksToBounds = true


    _ = seperator.anchor(storeCity.bottomAnchor, left: self.view.leftAnchor, bottom: nil, right: self.view.rightAnchor, topConstant: 8, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 1)

    view1.anchorToTop(map.bottomAnchor, left: self.view.leftAnchor, bottom: scrollView.bottomAnchor, right: self.view.rightAnchor)
    view1.heightAnchor.constraint(equalTo: self.view.heightAnchor, multiplier: 0.8).isActive = true

}`

回答1:


Move this code in viewWillLayoutSubviews:

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    goToMapsButton.layer.cornerRadius = 0.25 * goToMapsButton.frame.width
}

Or create your custom class for button with rounded corners:

class RoundedButton: UIButton {
    @IBInspectable var cornerRadius: CGFloat = 0

    override func layoutSubviews() {
        super.layoutSubviews()
        clipsToBounds = true
        layer.cornerRadius = cornerRadius
    }
}



回答2:


Give your button a frame. like that

goToMapsButton.frame = CGRect(x: xposition, y:yposition, width: widthyouwant, height: heightyouwant)

as currently your size of button is zero 0*0.5 = zero thats why its not applying any radius. give it frame and it will work...




回答3:


UIButton might have a background UIImage with rounded corners. It allowes you to set a background UIImage for each UIControlState of your UIButton.

open class UIButton : UIControl, NSCoding {

    open func setBackgroundImage(_ image: UIImage?, for state: UIControlState)
}

If your UIButton's size is determined at runtime and the radius is fixed - you can use a resizable image:

open class UIImage : NSObject, NSSecureCoding {

    open func resizableImage(withCapInsets capInsets: UIEdgeInsets) -> UIImage
}

On the gif below I use UIImage with size = CGSize(width: 7, height: 7), corner radius = 3 and cap insets = UIEdgeInsets(top: 3, left: 3, bottom: 3, right: 3)



来源:https://stackoverflow.com/questions/48716990/swift-cannot-round-the-corners-of-my-uibutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!