gpt4 book ai didi

ios - 使用约束条件时按钮的角不保持圆角

转载 作者:行者123 更新时间:2023-11-29 00:23:13 28 4
gpt4 key购买 nike

我在堆栈 View 中有两个按钮。我使用了 UIButton 的扩展来圆化外角。这适用于我在 Storyboard中设计的 7Plus,但一旦我在模拟器中运行较小的设备尺寸,它就会停止工作,我只能在任一按钮的左侧而不是右侧圆角。有什么想法吗?

在 7Plus 上 enter image description here

7 enter image description here

这些是我正在使用的扩展

extension CGSize{
init(_ width:CGFloat,_ height:CGFloat) {
self.init(width:width,height:height)
}
}
extension UIButton{
func roundOneSide(topCorner: UIRectCorner, bottomCorner: UIRectCorner){
let maskPAth1 = UIBezierPath(roundedRect: self.bounds,
byRoundingCorners: [topCorner , bottomCorner],
cornerRadii:CGSize(6.0, 6.0))
let maskLayer1 = CAShapeLayer()
maskLayer1.frame = self.bounds
maskLayer1.path = maskPAth1.cgPath
self.layer.mask = maskLayer1
}
}

我也试过下面的代码,没有用。只有当约束发挥作用时,它才能成功地绕过左侧的拐角。

    let maskLayer = CAShapeLayer()
maskLayer.path = UIBezierPath(roundedRect: view.bounds, byRoundingCorners: [.topLeft, .bottomRight], cornerRadii: CGSize(width: 6, height: 6)).cgPath
facebookBtn.layer.mask = maskLayer
facebookBtn.layer.masksToBounds = true

最佳答案

使用你使用 segment Control 作为 jerky said 或者试试下面的代码:

//登录按钮

   UIBezierPath *cornersPathLeft = [UIBezierPath bezierPathWithRoundedRect:buttonLogin.bounds  byRoundingCorners:(UIRectCornerBottomLeft|
UIRectCornerTopLeft) cornerRadii:CGSizeMake(5, 5)];

//Create a new layer to use as a mask

CAShapeLayer *maskLayerLeft = [CAShapeLayer layer];

// Set the path of the layer

maskLayerLeft.path = cornersPathLeft.CGPath;
buttonLogin.layer.mask = maskLayerLeft;

//FB按钮

    UIBezierPath *cornersPathRight = [UIBezierPath bezierPathWithRoundedRect:buttonFB.bounds  byRoundingCorners:(UIRectCornerTopRight|
UIRectCornerBottomRight) cornerRadii:CGSizeMake(5, 5)];

CAShapeLayer *maskLayerRight = [CAShapeLayer layer];
maskLayerRight.path = cornersPathRight.CGPath;
buttonFB.layer.mask = maskLayerRight;

注意:

do maskToBounds = true 它允许对图层产生影响。

-

MaskToBounds 和 ClipsToBounds 的区别

MaskToBounds

层的任何延伸到其边界之外的子层都将被裁剪到这些边界。在这种情况下,将该层视为其子层的窗口;窗口边缘之外的任何内容都将不可见。当 masksToBounds = NO 时,不会发生裁剪。

当此属性的值为 true 时,Core Animation 会创建一个隐式剪切蒙版,该蒙版与图层的边界相匹配,并包括任何角半径效果。如果还指定了 mask 属性的值,则将两个掩码相乘以获得最终掩码值。

ClipsToBounds

clipsToBounds 的用例更多地用于部分位于主视图之外的 subview 。

例如,我在其父级(矩形)UIView 的边缘有一个(圆形) subview 。如果将 clipsToBounds 设置为 YES,则只会显示一半的圆/ subview 。如果设置为 NO,将显示整个圆圈。刚遇到这个所以想分享

结论

MaskToBounds 应用于任何 View 的子层。就像这里的 OP 在按钮上添加了层,但它没有产生效果。我的意思是图层的边界不正确。

ClipToBounds 应用于任何 View 的 subview 。假设你有一个 View (说,viewBG),现在你添加了另一个 View (说,upperView),现在你不想看到 View 上部查看 viewBG 之外。 ViewUpper 始终限制在其父 View 内。所以在这种情况下,您必须使 clipstobounds 为真。

实践经验

快速尝试下面的代码

let viewBG : UIView = UIView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
viewBG.backgroundColor = UIColor.lightGray

self.view.addSubview(viewBG)

let viewUpper : UIView = UIView(frame: CGRect(x: -50, y: -50, width: 100, height: 100))
viewUpper.backgroundColor = UIColor.blue
viewBG.addSubview(viewUpper)

输出1. 当我完成 viewBG.clipsToBounds = false

enter image description here

  1. 当我完成 viewBG.clipsToBounds = true

enter image description here

关于ios - 使用约束条件时按钮的角不保持圆角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43796505/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com