斯威夫特不能绕过我的UIButton的角落

时间:2018-02-10 03:45:10

标签: ios swift uibutton cornerradius

我几乎看过每个stackoverflow解决方案,但由于一些奇怪的原因,我的按钮不会绕过角落。有人可以查看我看错了吗?

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

顺便说一句,我是在视图控制器的viewDidLoad部分中完成此操作,如果该信息有所不同。

以下是完整的viewDidLoadClass供参考:`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

}`

4 个答案:

答案 0 :(得分:4)

在viewWillLayoutSubviews中移动此代码:

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

或者为圆角按钮创建自定义类:

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

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

答案 1 :(得分:0)

给你的按钮一个框架。像那样

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

因为当前按钮的大小为零0 * 0.5 =零,这就是为什么它不应用任何半径。 给它框架,它会工作......

答案 2 :(得分:0)

UIButton可能有一个带圆角的背景UIImage。它允许您为UIImage的每个UIControlState设置背景UIButton

open class UIButton : UIControl, NSCoding {

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

如果在运行时确定UIButton的大小并且半径是固定的 - 您可以使用可调整大小的图像:

open class UIImage : NSObject, NSSecureCoding {

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

在下面的gif上,我使用尺寸= UIImage的{​​{1}},角半径= CGSize(width: 7, height: 7)和上限= 3

enter image description here

答案 3 :(得分:0)

我今天遇到了同样的问题,我也在使用 Anchor。嗯,对我来说它看起来像一个 one line stuff,从没想过它会需要那么多代码。

难点在于我们无法在UIButton属性闭包中获取frame.height或width,因为当时autolayout还没有计算大小。我做了一个测试来应用 viewDidLayoutSubviews 中的纯圆角。

这就是它的样子,尽管这些 UIButton 的声明是分开的。我真的不喜欢这种方式,我想把所有按钮声明代码放在同一个地方,就像一个闭包一样。但是我在互联网上找不到任何其他方法来做到这一点。

    let b1 = UIButton()
    let b2 = UIButton()
    let b3 = UIButton()
    let b4 = UIButton()

    override func viewDidLoad() {
        super.viewDidLoad()

        configureLayout()
    }

    private func configureLayout() {
        // disable large title
        navigationItem.largeTitleDisplayMode = .never
        
        let topLabel = UILabel()
        topLabel.text = "Import your music"
        topLabel.font = .systemFont(ofSize: 30, weight: .heavy)
        topLabel.numberOfLines = 0
        topLabel.textColor = .black
        topLabel.textAlignment = .center
        
        let stack = UIStackView()
        stack.translatesAutoresizingMaskIntoConstraints = false
        stack.axis = .vertical
        stack.spacing = 40
        stack.addArrangedSubview(topLabel)
        
        b1.setTitle("Local drive", for: .normal)
        b2.setTitle("Google", for: .normal)
        b3.setTitle("OneNote", for: .normal)
        b4.setTitle("DropBox", for: .normal)
        
        b1.backgroundColor = .myBlue
        b2.backgroundColor = .myGreen
        b3.backgroundColor = .systemPink
        b4.backgroundColor = .myPuple
        
        b1.tag = 1
        b2.tag = 2
        b3.tag = 3
        b4.tag = 4
        
        [b1, b2, b3, b4].forEach { b in
            b.titleLabel?.font = .systemFont(ofSize: 20, weight: .bold)
            b.setTitleColor(.white, for: .normal)
            b.setTitleColor(.gray, for: .highlighted)
            stack.addArrangedSubview(b)
            b.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
        }
        view.addSubview(stack)
        
        let g = view.safeAreaLayoutGuide
        NSLayoutConstraint.activate([
            stack.centerXAnchor.constraint(equalTo: g.centerXAnchor),
            stack.centerYAnchor.constraint(equalTo: g.centerYAnchor),
            stack.widthAnchor.constraint(equalTo: g.widthAnchor, multiplier: 0.75)
        ])
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        [b1, b2, b3, b4].forEach { b in
            b.layer.cornerRadius = b1.frame.height / 2
            b.layer.cornerCurve = .continuous
        }
    }

enter image description here