菜鸟教程小白 发表于 2022-12-11 17:54:16

ios - 仅将角半径设置为 UIView/UIButton 的特定侧


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

<p> <a href="/image/mqOU2.png" rel="noreferrer noopener nofollow"><img src="/image/mqOU2.png" alt="enter image description here"/></a> </p>

<p>我尝试使用 <code>UIBezierPath</code> 设置角半径,但它没有给出预期的结果。我还在 <code>UIView</code> 中添加了这三个 <code>UIButton</code> 并将角半径设置为容器 View 但没有用。</p>

<p>引用 <a href="https://stackoverflow.com/a/31233171/4886069" rel="noreferrer noopener nofollow">https://stackoverflow.com/a/31233171/4886069</a> , 以下是我在顶部和底部按钮上尝试过的代码:</p>

<pre><code>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
</code></pre>

<p>以下是我在容器 View 上尝试过的代码:</p>

<pre><code>containerView.layer.cornerRadius = 20
</code></pre>

<p>以下是我得到的输出:</p>

<p> <a href="/image/ipQdJ.png" rel="noreferrer noopener nofollow"><img src="/image/ipQdJ.png" alt="enter image description here"/></a> </p>

<p>任何帮助表示赞赏。谢谢</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>将 <strong>byRoundingCorners: [.topLeft]</strong> 选项中的 topRight、bottomLeft 和 bottomRight 用于其他角以获得预期结果。 </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 仅将角半径设置为 UIView/UIButton 的特定侧,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/40169745/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/40169745/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 仅将角半径设置为 UIView/UIButton 的特定侧