我的登录视图有一个子视图,它有一个UIActivityView和一个UILabel,上面写着“Signing In…”这个子视图有不圆角的角。我怎样才能把它们变圆呢?
有什么办法在我的xib里面做吗?
我的登录视图有一个子视图,它有一个UIActivityView和一个UILabel,上面写着“Signing In…”这个子视图有不圆角的角。我怎样才能把它们变圆呢?
有什么办法在我的xib里面做吗?
当前回答
- 斯威夫特
在SwiftUI中,你可以直接在任何你想要的视图上使用拐角半径修饰器。 比如这个问题:
Text("Signing In…")
.padding(16)
.background(Color.red)
.cornerRadius(50)
注意,没有更多的菱形半径,所以即使你设置角半径超过高度的一半,它也会平滑圆润。
查看这个答案,了解如何在SwiftUI中圆角
其他回答
现在你可以在UIView中使用一个快速的类别(代码如下图所示),在@IBInspectable中显示在故事板上的结果(如果你正在使用类别,只使用拐角半径,而不是图层。cornerRadius作为关键路径。
extension UIView {
@IBInspectable var cornerRadius: CGFloat {
get {
return layer.cornerRadius
}
set {
layer.cornerRadius = newValue
layer.masksToBounds = newValue > 0
}
}
}
在Swift 4.2和Xcode 10.1中
let myView = UIView()
myView.frame = CGRect(x: 200, y: 200, width: 200, height: 200)
myView.myViewCorners()
//myView.myViewCorners(width: myView.frame.width)//Pass View width
view.addSubview(myView)
extension UIView {
//If you want only round corners
func myViewCorners() {
layer.cornerRadius = 10
layer.borderWidth = 1.0
layer.borderColor = UIColor.red.cgColor
layer.masksToBounds = true
}
//If you want complete round shape, enable above comment line
func myViewCorners(width:CGFloat) {
layer.cornerRadius = width/2
layer.borderWidth = 1.0
layer.borderColor = UIColor.red.cgColor
layer.masksToBounds = true
}
}
- 斯威夫特
在SwiftUI中,你可以直接在任何你想要的视图上使用拐角半径修饰器。 比如这个问题:
Text("Signing In…")
.padding(16)
.background(Color.red)
.cornerRadius(50)
注意,没有更多的菱形半径,所以即使你设置角半径超过高度的一半,它也会平滑圆润。
查看这个答案,了解如何在SwiftUI中圆角
你可以使用下面的自定义UIView类,它也可以改变边框颜色和宽度。因为这是IBDesignalbe,你也可以在界面构建器中更改属性。
import UIKit
@IBDesignable public class RoundedView: UIView {
@IBInspectable var borderColor: UIColor = UIColor.white {
didSet {
layer.borderColor = borderColor.cgColor
}
}
@IBInspectable var borderWidth: CGFloat = 2.0 {
didSet {
layer.borderWidth = borderWidth
}
}
@IBInspectable var cornerRadius: CGFloat = 0.0 {
didSet {
layer.cornerRadius = cornerRadius
}
}
}
你也可以使用图像:
UIImage *maskingImage = [UIImage imageNamed:@"bannerBarBottomMask.png"];
CALayer *maskingLayer = [CALayer layer];
maskingLayer.frame = CGRectMake(-(self.yourView.frame.size.width - self.yourView.frame.size.width) / 2
, 0
, maskingImage.size.width
, maskingImage.size.height);
[maskingLayer setContents:(id)[maskingImage CGImage]];
[self.yourView.layer setMask:maskingLayer];