下面给出的类运行良好
@IBDesignable class iButton : UIButton {
@IBInspectable var cornerRadius : CGFloat = 0.0{
didSet{
layer.cornerRadius = cornerRadius
}
}}但问题是,当我在属性检查器中为大小为(宽度: 70,高度70)的按钮设置cornerRadius值35时。我在故事板上得到了圆角按钮,但在模拟器上运行时,它不是圆形的,而是圆角的矩形。
我在xCode上的设计视图是iPhone-SE,并在iPhone-7+模拟器上模拟。
我还通过在大小检查器上设置高度和宽度来启用自动调整大小。


据我所知,转角半径应该是宽度的一半。当按钮通过自动调整大小来调整大小时,为什么不会调整角半径的大小。我该如何解决这个问题呢?
提前谢谢。
发布于 2017-07-20 20:02:07
import UIKit
@IBDesignable
class RoundedCornerButton: UIButton {
override func drawRect(rect: CGRect) {
self.clipsToBounds = true
}
@IBInspectable var cornerRadius: CGFloat = 0 {
didSet {
self.layer.cornerRadius = cornerRadius
}
}
}或者您可以查看此链接了解更多信息
发布于 2017-07-20 15:06:55
谢谢你,我有答案了。
我们还需要扩展cornerRadius。
layer.cornerRadius = cornerRadius * (UIScreen.main.bounds.width/320.0) //320.0 be my width of the screen that i designed on storyboardhttps://stackoverflow.com/questions/45191451
复制相似问题