我想创建一个带有渐变边框的按钮,在我将GradientButton
嵌套到StackView
中之前,一切正常。然后,我得到水平StackView
中最后一个按钮的结果。为什么子视图总是放错最后一项的位置?
这是我创建的GradientButton:
import Foundation
import UIKit
@IBDesignable class GradientButton: UIButton {
@IBInspectable var startColor: UIColor = UIColor.momaBackgroundGradient.begin
@IBInspectable var endColor: UIColor = UIColor.momaBackgroundGradient.end
@IBInspectable var borderWidth: CGFloat = 2
@IBInspectable var cornerRadius: CGFloat = 4
override func awakeFromNib() {
super.awakeFromNib()
let gradientBorder = UIView(frame: self.frame)
gradientBorder.configureGradient(withColors: [self.startColor, self.endColor], isHorizontal: true)
gradientBorder.mask(withPath: UIBezierPath(roundedRect: self.frame.insetBy(dx: self.borderWidth, dy: self.borderWidth), cornerRadius: self.frame.height/2), inverse: true)
gradientBorder.layer.cornerRadius = self.frame.height/2
gradientBorder.layer.masksToBounds = true
self.addSubview(gradientBorder)
}
}
最佳答案
梯度视图在init上使用它的父节点的frame
。将其更改为bounds
,如下所示:
let gradientBorder = UIView(frame: self.bounds)
关于ios - 当父 View 位于StackView中时,UIView的 subview 放错了位置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52049726/