简体   繁体   English

圆角按钮被切断的问题

[英]Trouble with Rounded corner buttons being cut off

I have a custom button class I use to get rounded corners.我有一个用于获得圆角的自定义按钮类。 The class works for some hard coded lengths/widths, but doesn't work for others.该类适用于某些硬编码的长度/宽度,但不适用于其他人。 I have not been able to get it to work at all with dynamically sized buttons.我根本无法让它与动态大小的按钮一起工作。 As you can see by the picture, the button is cut off in various ways.正如您在图片中看到的那样,该按钮以各种方式被切断。 Any idea what I'm doing wrong?知道我做错了什么吗?

class CustomButton: UIButton{
    
     
       override init(frame: CGRect) {
           super.init(frame: frame)
           setupButton()
       }
       
       
       required init?(coder aDecoder: NSCoder) {
           super.init(coder: aDecoder)
           setupButton()
       }
       
       
       func setupButton() {
           setTitleColor(.white, for: .normal)
           layer.cornerRadius   = frame.size.height/2
           layer.masksToBounds = true
       }
       
}

在此处输入图片说明

Thanks for any help.谢谢你的帮助。

This happens because you set the corners just after button's creation using initial frame.发生这种情况是因为您在使用初始帧创建按钮之后设置了角。 Button's frame are changing during layout, but its corners are not redrawn.按钮的框架在布局过程中会发生变化,但它的角不会重新绘制。 Just add setupButton call to layoutSubviews:只需将 setupButton 调用添加到 layoutSubviews:

override func layoutSubviews() {
    super.layoutSubviews()
    setupButton()
}

Also I recommend you to use UIBezierPath approach instead of cornerRadius due to possible performance issues.此外,由于可能存在性能问题,我建议您使用 UIBezierPath 方法而不是cornerRadius。

Although you are didn't share the issue code, Seems like you have issues with the layer!虽然您没有分享问题代码,但您似乎对图层有问题! CALayer is Not auto-resizing due to frame changes, so it sets by the initial setup and stays the same.由于帧更改, CALayer不会自动调整大小,因此它由初始设置设置并保持不变。 So first try to make it more like a view.所以首先试着让它更像一个视图。 The following code gives you a customizable gradient view that is reacting to any layout call (even right in the Storyboard!):以下代码为您提供了一个可自定义的渐变视图,它可以对任何布局调用做出反应(甚至在 Storyboard 中!):

@IBDesignable
final class GradientView: UIView {

    @IBInspectable var firstColor: UIColor = .clear { didSet { updateView() } }
    @IBInspectable var secondColor: UIColor = .clear { didSet { updateView() } }

    @IBInspectable var startPoint: CGPoint = CGPoint(x: 0, y: 0) { didSet { updateView() } }
    @IBInspectable var endPoint: CGPoint = CGPoint(x: 1, y: 1) { didSet { updateView() } }

    override class var layerClass: AnyClass { get { CAGradientLayer.self } }

    override func layoutSubviews() {
        super.layoutSubviews()
        updateView()
        layer.frame = bounds
    }

    private func updateView() {
        let layer = self.layer as! CAGradientLayer
        layer.colors = [firstColor, secondColor].map {$0.cgColor}
        layer.startPoint = startPoint
        layer.endPoint = endPoint
    }
}

预览

Then you can add it to your custom view and frame it to the parent (or any other layout method that you like):然后您可以将其添加到您的自定义视图并将其框架到父视图(或您喜欢的任何其他布局方法):

class CustomButton: UIButton{

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupButton()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupButton()
    }

    private lazy var gradientView: GradientView = {
        // Setup the gradient here
        let gradient = GradientView(frame: frame)
        gradient.startPoint = .init(x: 0, y: 0)
        gradient.endPoint = .init(x: 1, y: 1)
        gradient.firstColor = .red
        gradient.secondColor = .orange
        return gradient
    }()

    private func addLayerIfNeeded() {
        guard gradientView.superview == nil else { return }
        addSubview(gradientView)
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        gradientView.frame =  bounds
    }

    func setupButton() {
        addLayerIfNeeded()
        setTitleColor(.white, for: .normal)
    }
}

预览

I was able to fix the issue by applying the solutions in the article to my buttons as opposed to a view.我能够通过将文章中的解决方案应用于我的按钮而不是视图来解决这个问题。 This enabled dynamic resizing with gradients.这启用了渐变的动态调整大小。

Why gradient doesn't cover the whole width of the view 为什么渐变没有覆盖视图的整个宽度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM