ios - 尝试在 Swift 中制作圆形 UIView 时出错

标签 ios iphone swift uiview cornerradius

我的屏幕布局中有一个 UIImageView,它会根据容器 View 尺寸动态调整大小。我想在圆形 View 中转换它,我正在使用以下代码:

let dSize: CGFloat = min(imageview.frame.height, imageview.frame.width)
imageview.frame = CGRectMake(0, 0, dSize, dSize)
imageview.clipsToBounds = true
imageview.layer.cornerRadius = dSize/2.0
imageview.layer.borderWidth = 1.0
imageview.layer.borderColor = UIColor.whiteColor().CGColor

问题是生成的 View 并不总是圆形的。为了实现这种效果,我必须在 iPhone 4 上将 dSize 除以 2.0,在 iPhone 5/5s 上除以 1.7,在 1.5 在 iPhone 6/6s 上。我不知道我做错了什么。

最佳答案

你能试试这个吗

//To make your imageView circular
imageView.layer.cornerRadius = imageView.frame.size.height/2;
imageView.layer.masksToBounds = YES;

//To add border
imageView.layer.borderColor = [UIColor whiteColor].CGColor;
imageView.layer.borderWidth = 2;

关于ios - 尝试在 Swift 中制作圆形 UIView 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30667875/

相关文章:

objective-c - 更改 iOS 中的坐标方向

iphone - 如何避免将 NSData 的内容保留在事件内存中?

ios - 如何检查移动包数据是否可用的互联网..?

ios - 如何使用 XCDYouTubeKit for Swift 播放播放列表中的下一个 youtube 视频?

ios - Swift - 如何获取核心数据中的最后插入 ID,将其存储在 NSUserDefault 和谓词中

ios - 执行后台获取时不一直返回 UIBackgroundFetchResultNewData 的缺点是什么?

ios - 调整自定义 UINavigationController 的 titleView

iphone - UIActionSheet 高度在 3.5 英寸设备上为 4 英寸

ios - Swift/SpriteKit 帮助 : Increase CGFloat continuously to allow continuous rotation around a fixed point

ios - 如何获取注释放置 iOS 的时间