【问题标题】:Set corner radius to particular side of UIView/UIButton only仅将角半径设置为 UIView/UIButton 的特定侧
【发布时间】:2016-10-21 06:27:40
【问题描述】:

我想为我的 UI 元素设置圆角半径,如附加快照所示:

我尝试使用UIBezierPath 设置拐角半径,但它没有给出预期的结果。我还在UIView 中添加了这三个UIButtons,并将角半径设置为容器视图但没有用。

参考https://stackoverflow.com/a/31233171/4886069,以下是我在顶部和底部按钮上尝试过的代码:

let rectShape = CAShapeLayer()
rectShape.bounds = self.scanButton.frame
rectShape.position = self.scanButton.center
rectShape.path = UIBezierPath(roundedRect: self.scanButton.bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: 20, height: 20)).cgPath
self.scanButton.layer.mask = rectShape

let rectShape1 = CAShapeLayer()
rectShape1.bounds = self.manualButton.frame
rectShape1.position = self.manualButton.center
rectShape.path = UIBezierPath(roundedRect: self.manualButton.bounds, byRoundingCorners: [.bottomLeft, .bottomRight], cornerRadii: CGSize(width: 20, height: 20)).cgPath
self.manualButton.layer.mask = rectShape1

以下是我在容器视图上尝试过的代码:

containerView.layer.cornerRadius = 20

以下是我得到的输出:

任何帮助表示赞赏。谢谢

【问题讨论】:

  • 是否有 2 个 uiview 用于一个取消,一个用于 3 个选项?
  • 没有。取消按钮不在容器视图中。取消按钮角半径工作正常,因为 cancelButton.layer.cornerRadius = 15
  • 扫描按钮看起来与我期望它给出的面具完全一样。有什么理由不只是将 UIAlertController 与操作表样式一起使用?
  • UIAlertController 将从底部弹出并粘在 UIView 的底部。我希望它位于屏幕中心

标签: ios uiview uibutton swift3 cornerradius


【解决方案1】:

byRoundingCorners: [.topLeft] 选项中为其他角使用 topRight、bottomLeft 和 bottomRight 以获得预期结果。

【讨论】:

    【解决方案2】:

    此代码适用于 swift 2.0 只需将此代码转换为 swift 3.0

    cancelbnutton.layer.cornerRadius = 10.0
     cancelbnutton.clipsToBounds = true
     let rectShape = CAShapeLayer()
     rectShape.bounds = scanbutton.frame
     rectShape.position = scanbutton.center
    
     rectShape.path = UIBezierPath(roundedRect: scanbutton.bounds, byRoundingCorners: [UIRectCorner.TopLeft , UIRectCorner.TopRight], cornerRadii: CGSizeMake(10, 10)).CGPath
     scanbutton.layer.mask = rectShape
    
     let rectShape1 = CAShapeLayer()
     rectShape1.bounds = manualbutton.frame
     rectShape1.position = manualbutton.center
    
     rectShape1.path = UIBezierPath(roundedRect: manualbutton.bounds, byRoundingCorners: [UIRectCorner.BottomLeft , UIRectCorner.BottomRight], cornerRadii: CGSizeMake(10, 10)).CGPath
     manualbutton.layer.mask = rectShape1
    

    【讨论】:

    • 我只是将上面的代码提供给 UIButton 和它的显示就像你想要的一样
    【解决方案3】:

    使用它来给出圆角半径

    let Path = UIBezierPath(roundedRect: button.bounds, byRoundingCorners: [UIRectCorner.TopLeft , UIRectCorner.BottomLeft], cornerRadii: CGSizeMake(1.0, 1.0)).CGPath
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-07
      • 2017-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多