ios 如何设置imageView的角半径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27861383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set corner radius of imageView?
提问by theDC
In Objective-C such line
在 Objective-C 这样的行中
self.mainImageView.layer.cornerRadius = CGRectGetWidth(self.mainImageView.frame)/4.0f;
does its job, I tried it in Swift using analogy
完成它的工作,我使用类比在 Swift 中尝试过
self.mainImageView.layer.cornerRadius = CGRectGetWidth(self.mainImageView.frame)/4.0
and it doesn't change anything, the corners are the same as before. Moreover, Xcode does not show any syntax errors. Does Swift support any other way to reach this goal? I checked some other threads here and usually it's getting done in Swift in the way showed above.
它没有改变任何东西,角落和以前一样。此外,Xcode 不会显示任何语法错误。Swift 是否支持任何其他方式来达到这个目标?我在这里检查了一些其他线程,通常它是按照上面显示的方式在 Swift 中完成的。
回答by DarthMike
Layer draws out of clip region, you need to set it to mask to bounds:
图层绘制出剪辑区域,您需要将其设置为蒙版到边界:
self.mainImageView.layer.masksToBounds = true
From the docs:
从文档:
By default, the corner radius does not apply to the image in the layer's contents property; it applies only to the background color and border of the layer. However, setting the masksToBounds property to true causes the content to be clipped to the rounded corners
默认情况下,角半径不适用于图层内容属性中的图像;它仅适用于图层的背景颜色和边框。但是,将 maskToBounds 属性设置为 true 会导致内容被裁剪到圆角
回答by theDC
There is one tiny difference in Swift 3.0and Xcode8
Swift 3.0和Xcode8有一个微小的区别
Whenever you want to apply corner radius to UIView, make sure you call
yourUIView.layoutIfNeeded()
before calling cornerRadius
.
每当您想将圆角半径应用于 UIView 时,请确保
yourUIView.layoutIfNeeded()
在调用cornerRadius
.
Otherwise, it will return the default value for UIView's height and width (1000.0) which will probably make your View disappear.
否则,它将返回 UIView 的高度和宽度的默认值 (1000.0),这可能会使您的 View 消失。
Always make sure that all effects that changes the size of UIView (Interface builder constraints etc) are applied before setting any layer properties.
在设置任何图层属性之前,请始终确保应用了所有更改 UIView 大小的效果(界面构建器约束等)。
Example of UIView class implementation
UIView 类实现示例
class BadgeView: UIView {
override func awakeFromNib() {
self.layoutIfNeeded()
layer.cornerRadius = self.frame.height / 2.0
layer.masksToBounds = true
}
}
回答by Marcelo Tadeu
回答by Anbu.Karthik
try this
尝试这个
self.mainImageView.layer.cornerRadius = CGRectGetWidth(self.mainImageView.frame)/4.0
self.mainImageView.clipsToBounds = true
回答by Amit Jagesha シ
Marked with @IBInspectable
in swift (or IBInspectable in Objective-C), they are easily editable in Interface Builder's attributes inspector panel.
You can directly set borderWidth,cornerRadius,borderColorin attributes inspector
用@IBInspectable
swift标记(或 Objective-C 中的 IBInspectable),它们可以在 Interface Builder 的属性检查器面板中轻松编辑。
您可以在属性检查器中直接设置borderWidth、cornerRadius、borderColor
extension UIView {
@IBInspectable var cornerRadius: CGFloat {
get{
return layer.cornerRadius
}
set {
layer.cornerRadius = newValue
layer.masksToBounds = newValue > 0
}
}
@IBInspectable var borderWidth: CGFloat {
get {
return layer.borderWidth
}
set {
layer.borderWidth = newValue
}
}
@IBInspectable var borderColor: UIColor? {
get {
return UIColor(cgColor: layer.borderColor!)
}
set {
layer.borderColor = borderColor?.cgColor
}
}
}
回答by Nicolas Desormiere
in swift 3 'CGRectGetWidth' has been replaced by property 'CGRect.width'
在 swift 3 'CGRectGetWidth' 已被属性 'CGRect.width' 取代
view.layer.cornerRadius = view.frame.width/4.0
view.clipsToBounds = true
回答by Gaurav Rami
Swift 3, Xcode 8, iOS 10
斯威夫特 3、Xcode 8、iOS 10
DispatchQueue.main.async {
self.mainImageView.layer.cornerRadius = self.mainImageView.bounds.size.width / 2.0
self.mainImageView.clipsToBounds = true
}
回答by Claudia Fitero
The easiest way is to create an UIImageView subclass (I have tried it and it's working perfectly on iPhone 7 and XCode 8):
最简单的方法是创建一个 UIImageView 子类(我已经尝试过了,它在 iPhone 7 和 XCode 8 上运行良好):
class CIRoundedImageView: UIImageView {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func awakeFromNib() {
self.layoutIfNeeded()
layer.cornerRadius = self.frame.height / 2.0
layer.masksToBounds = true
}
}
and then you can also set a border:
然后你还可以设置边框:
imageView.layer.borderWidth = 2.0
imageView.layer.borderColor = UIColor.blackColor().CGColor
回答by fredericdnd
I created an UIView
extension which allows to round specific corners :
我创建了一个UIView
允许圆特定角的扩展:
import UIKit
enum RoundType {
case top
case none
case bottom
case both
}
extension UIView {
func round(with type: RoundType, radius: CGFloat = 3.0) {
var corners: UIRectCorner
switch type {
case .top:
corners = [.topLeft, .topRight]
case .none:
corners = []
case .bottom:
corners = [.bottomLeft, .bottomRight]
case .both:
corners = [.allCorners]
}
DispatchQueue.main.async {
let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
self.layer.mask = mask
}
}
}
回答by Chinedu Ofor
import UIKit
class BorderImage: UIImageView {
override func awakeFromNib() {
self.layoutIfNeeded()
layer.cornerRadius = self.frame.height / 10.0
layer.masksToBounds = true
}
}
Based on @DCDC's answer
基于@DCDC 的回答