【问题标题】:UIButton border cut when corner radius is given给定角半径时UIButton边框切割
【发布时间】:2015-06-26 17:47:36
【问题描述】:

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

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

  [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 这个函数可以创建特定的边角半径。在这种情况下谁能帮帮我。

【问题讨论】:

    标签: ios objective-c uibutton rounded-corners


    【解决方案1】:

    请尝试以下代码。我认为它将满足您的要求。

    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];
    

    输出:

    【讨论】:

      【解决方案2】:

      而不是设置 setroundCornersOnView 。您可以将cornerRadius 设置为按钮。 试试下面的代码。 [btn_Newest.layer setCornerRadius:5];

      【讨论】:

      • 感谢您的重播,但我要求按钮应该看起来像屏幕截图。虽然我尝试了你的回答。
      猜你喜欢
      • 2019-03-03
      • 1970-01-01
      • 2017-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-31
      • 2013-01-17
      相关资源
      最近更新 更多