ios - UIView 不显示圆形

标签 ios swift rounded-corners

为了制作圆形 UIView,我使用了 cornerRadius 属性。

我有一个尺寸为 79*158 的 UIView。

redView.layer.cornerRadius = redView.frame.size.height/2
redView.layer.masksToBounds = true

它显示椭圆而不是圆:

It shows elipse instead of circle

是否有任何解决方法或它是否仅适用于方形类型(例如 UIView(100*100))?

如果它动态调整大小我没问题。

最佳答案

使用这个...

    func makeCircle (view: UIView) {
        view.clipsToBounds = true
        let height = view.frame.size.height
        let width = view.frame.size.width
        let newHeight = min(height, width) // use "max" if you want big circle

        var rectFrame = view.frame
        rectFrame.size.height = newHeight
        rectFrame.size.width = newHeight
        view.frame = rectFrame
        view.layer.cornerRadius = newHeight/2
    }

像这样使用:

@IBOutlet var rectView: UIView!

override func viewDidLoad() {
        super.viewDidLoad()

        makeCircle(view: rectView)
    }

关于ios - UIView 不显示圆形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49313267/

相关文章:

ios - 尝试为新闻源创建 timeAgo 函数,但它只显示 "0 seconds ago"

ios - 仅为 UITableView 中的一个部分启用编辑模式

ios - 有没有比这个更好的解决 UITableview 单元格圆角的方法?

django - 无法快速上传嵌套的多部分表单数据

ios - 带有圆角和投影的 UIView?

css - IE7中的圆 Angular div问题

internet-explorer - CSS 3 PIE : Not working inside an absolutely positioned (popup) element? 鼠标悬停时移动?

iphone - 如何使用 setObject : forKey where Key is also a NSMutableDictionary?

ios - UIstoryboard:Modal segues 的 Animates 选项在 6.0 之前的 iOS 版本上不可用

iphone - 是否有 APN 的替代方案