ios - 圆形 UIImageView 在某些设备上是方形的

标签 ios iphone swift cocoa-touch uiview

我有一个 UIView,其中有一个 UIImageView

我将 UIImageView 设置为圆形,并为其添加边框,如下所示:

self.profilePicImageView.layer.cornerRadius = self.profilePicImageView.frame.size.height / 2
self.profilePicImageView.layer.masksToBounds = true

self.profilePicImageView.layer.borderColor = UIColor.white.cgColor
self.profilePicImageView.layer.borderWidth = 3

在我初始化 viewController 中的 View 之后以及将其添加为 subview (名为 setupUI() 的函数)之前,它会被调用。

它在大多数设备上工作得很好,但在大屏幕设备(iPhone 6/6s/7 Plus 和 iPhone X)上我得到了正确的边框,但图像本身不是圆形的。

请参阅示例:

普通 iPhone:

Regular

大型 iPhone(iPhone 6/6s/7 Plus 和 iPhone X):

Large

知道问题是什么以及如何解决它吗?

谢谢!!

最佳答案

只需尝试此代码,它会对您有所帮助。

imageView.layer.cornerRadius = imageHeight/2
imageView.layer.masksToBounds = true
imageView.contentMode = .scaleAspectFill

关于ios - 圆形 UIImageView 在某些设备上是方形的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46988121/

相关文章:

ios - 库未加载 : @rpath/Alamofire. 框架/Alamofire

iphone - UIWebView 电影播放器​​被解雇的 iOS 6 错误

ios - 苹果健康 : export health data automatically

iphone - Objective-c 现代运行时在接口(interface) block 中同时使用属性和 ivars

ios - 方法 openURL :options:completionHandler compatibility in objective c

ios - 图表显示不正确? iOS 图表库

swift - 401 alamofire 获取带有 header 的请求

iphone - iOS 加速度计在后台(无限)

iphone - iOS 崩溃日志捕获、调试信息...捕获并通过电子邮件发送给开发团队

swift - 直接赋值UnsafePointer