【发布时间】:2014-04-14 12:57:51
【问题描述】:
我目前正在查看属性为 addMessageLabel.layer.cornerRadius = 5.0f; 的 UILabel 在安装了 iOS 7.0 的设备上,它有圆角。在安装了 iOS 7.1 的设备上,它没有圆角。
这只是 iOS 7.1 的一个错误吗?
【问题讨论】:
标签: ios uiview ios7 uilabel cornerradius
我目前正在查看属性为 addMessageLabel.layer.cornerRadius = 5.0f; 的 UILabel 在安装了 iOS 7.0 的设备上,它有圆角。在安装了 iOS 7.1 的设备上,它没有圆角。
这只是 iOS 7.1 的一个错误吗?
【问题讨论】:
标签: ios uiview ios7 uilabel cornerradius
将属性clipsToBounds 设置为true
addMessageLabel.clipsToBounds = true
【讨论】:
我认为设置圆角半径的最佳方法是:
并确保选中“剪辑子视图”:
勾选“Clip Subviews”等于代码addMessageLabel.clipsToBounds = YES;。
【讨论】:
试试下面的,
[[addMessageLabel layer] setCornerRadius:5.0f];
[[addMessageLabel layer] setMasksToBounds:YES];
//or
[addMessageLabel setClipsToBounds:YES];
斯威夫特
addMessageLable.layer.cornerRadius = 5.0
addMessageLable.layer.masksToBounds = true
//or
addMessageLable.layer.clipsToBounds = true
【讨论】:
我的问题有点不同。
而我做过 btn.clipsToBounds = true
我没有设置这样做:
btn.layer.cornerRadius = 20
因为我有不同的屏幕尺寸。相反,我跟着this 回答并做了:
override func layoutSubviews() {
seeMoreButton.layer.cornerRadius = seeMoreButton.bounds.size.height / 2
}
因为我忘记添加super.layoutSubviews(),所以它不起作用。正确的代码是:
override func layoutSubviews() {
super.layoutSubviews()
seeMoreButton.layer.cornerRadius = seeMoreButton.bounds.size.height / 2
}
【讨论】:
我已经尝试了下面的一个,我得到了一个成功的输出。
yourlabelname.layer.cornerRadius = 10.0f;
[yourlabelname setClipsToBounds:YES];
还有什么阻碍你的吗?
【讨论】:
clipsToBounds 被默认为YES,所以[yourlabelname setClipsToBounds:YES]; 这行不在我的原始代码中。
//works perfect in Swift 2.0 for a circular or round image
@IBOutlet var theImage: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
//Make sure the width and height are same
self.theImage.layer.cornerRadius = self.theImage.frame.size.width / 2
self.theImage.layer.borderWidth = 2.0
self.theImage.layer.borderColor = UIColor.whiteColor().CGColor
self.theImage.clipsToBounds = true
}
【讨论】:
yourlabelname.layer.cornerRadius = yourlabelname.frame.size.width/2;
[yourlabelname setClipsToBounds:YES];
确保您正在检查适当的部署目标。
【讨论】:
添加以下代码作为 UIView 的扩展
//// Story board Extra Feature for create border radius, border width and border Color
extension UIView {
/// corner radius
@IBInspectable var borderColor: UIColor? {
set {
layer.borderColor = newValue!.cgColor
}
get {
if let color = layer.borderColor {
return UIColor(cgColor: color)
} else {
return nil
}
}
}
@IBInspectable var borderWidth: CGFloat {
set {
layer.borderWidth = newValue
}
get {
return layer.borderWidth
}
}
@IBInspectable var cornerRadius: CGFloat {
set {
layer.cornerRadius = newValue
clipsToBounds = newValue > 0
}
get {
return layer.cornerRadius
}
}
}
之后,您将在界面生成器本身中获得以下属性。!
【讨论】: