ios - 当给定角半径时 UIButton 边框切割

标签 ios objective-c uibutton rounded-corners

我创建了具有特定位置角半径的UIButton,这意味着左按钮有左上角和左下角半径,右按钮有右上角和右下角半径我已经在Xib中使用了按钮并给出了边框,但我从一些中剪切了我附上了下面的屏幕截图。 enter image description here

查看右侧角边框切割。这是我的代码

  [btn_Newest setBackgroundColor:RGB(28.0, 91.0, 161.0, 1.0)];
    [btn_Newest.layer setBorderWidth:1.5];
    [btn_Newest.layer setBorderColor:RGB(28.0, 91.0, 161.0, 1.0).CGColor];
    [btn_Newest setClipsToBounds:YES];
    btn_Newest = (UIButton *)[self setroundCornersOnView:btn_Newest onTopLeft:YES topRight:NO bottomLeft:YES bottomRight:NO radius:7.0];


    [btn_Popular setBackgroundColor:viewTopBar.backgroundColor];
    [btn_Popular.layer setBorderWidth:1.5];
    [btn_Popular.layer setBorderColor:RGB(28.0, 91.0, 161.0, 1.0).CGColor];
    [btn_Popular setClipsToBounds:YES];
    btn_Popular = (UIButton *)[self setroundCornersOnView:btn_Popular onTopLeft:NO topRight:YES bottomLeft:NO bottomRight:YES radius:7.0];

setroundCornersOnView此函数创建特定的侧角半径。在这种情况下任何人都可以帮助我吗?

最佳答案

请尝试以下代码。我想它会满足你的要求。

UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:btnPush.bounds byRoundingCorners:(UIRectCornerTopRight | UIRectCornerBottomRight) cornerRadii:CGSizeMake(7.0, 7.0)];

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.view.bounds;
maskLayer.path  = maskPath.CGPath;
btnPush.layer.mask = maskLayer;

CAShapeLayer *borderLayer = [[CAShapeLayer alloc] init];
borderLayer.frame = self.view.bounds;
borderLayer.path  = maskPath.CGPath;
borderLayer.lineWidth   = 1.5f;
borderLayer.strokeColor = [UIColor blackColor].CGColor;
borderLayer.fillColor   = [UIColor clearColor].CGColor;
[btnPush.layer addSublayer:borderLayer];

输出:

enter image description here

关于ios - 当给定角半径时 UIButton 边框切割,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29740480/

相关文章:

iphone - 以编程方式获取结合 OpenGL 和 UIKit 元素的屏幕截图

ios - 禁用和启用状态的 UIButton tintColor?

ios - 如何用多个逗号分隔 NSString?

iphone - 如何在使用 AVAudioPlayer 的应用程序的多任务栏中启用音频控件?

ios - swift:如何调整 TableView 的高度以适应 super View 的高度

ios - UIButton 如何同时监听对其他按钮的更改、对 UILabel 文本的更改以及对 UITextView 的更改

ios - 循环遍历值事件类型并使用 Firebase 快速创建值 UIButton 的 titleLabel

javascript - react native ListView 项目 ZIndex

iphone - UITouch 移动速度检测

iphone - 如何在 UIDatePicker 中记住并加载选定的日期?