gpt4 book ai didi

iphone - 带有圆角和边框的 UIButton

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:08:01 24 4
gpt4 key购买 nike

我想在 UIButton 上设置边框,只在一侧设置圆角

我使用这段代码:

//set rounded corners on top
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.scanButton.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(10.0, 10.0)];

CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = self.scanButton.bounds;
maskLayer.path = maskPath.CGPath;

self.scanButton.layer.mask = maskLayer;

// set border
self.scanButton.layer.borderColor = [UIColor blackColor].CGColor;
self.scanButton.layer.borderWidth = 2.0f;
[self.scanButton.layer setMasksToBounds:YES];

enter image description here

如何在顶角制作合适的边框?

最佳答案

swift 2.0版本

边框的角被剪掉了,因为您可能已经在 UIView 本身上设置了 View 的背景颜色。更好的方法是直接在图层上设置填充颜色。这可以这样做:

let button = ... // your existing UIButton
let borderLayer = CAShapeLayer()
borderLayer.strokeColor = UIColor.blackColor().CGColor
borderLayer.fillColor = UIColor.yellowColor().CGColor
borderLayer.borderWidth = 2.0

let borderPath = UIBezierPath(roundedRect: button.bounds,
byRoundingCorners: [.TopLeft, .TopRight],
cornerRadii: CGSize(width:10.0, height: 10.0))
borderLayer.path = borderPath.CGPath

button.layer.insertSublayer(borderLayer, atIndex: 0)

上面的代码会给你这个没有剪角的结果:

example button

我已经使用这种方法组合了一个 UIButton 子类,其中可以分别在按钮的每个角上设置边框半径,请在此处查看:https://gist.github.com/alexpls/95279b3f9a2da7f2d7bf ,希望对您有所帮助!

关于iphone - 带有圆角和边框的 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19229494/

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