swift - 在 main.storyboard 中,圆形按钮未加载
问题描述
我在 Swift 中添加了 RoundButton 类,代码如下:
//
// RoundButton.swift
//
import UIKit
@IBDesignable
class RoundButton: UIButton {
@IBInspectable var roundButton : Bool = false {
didSet {
if roundButton == true {
layer.cornerRadius = frame.height / 2
}
}
}
override func prepareForInterfaceBuilder() {
if roundButton == true {
layer.cornerRadius = frame.height / 2
}
}
}
然后我在按钮属性中激活了设计。在 main.storyboard 中,按钮是方形的,但在构建中,按钮是圆形的。它出现了这个错误:
file:///Users/anonymoushuman/Documents/Apps/Taschenrechner/Taschenrechner/Base.lproj/Main.storyboard:错误:IB Designables:无法渲染和更新 ViewController 的自动布局状态(BYZ-38-t0r):捆绑包无法加载“Taschenrechner.app”,因为无法找到其可执行文件。
解决方案
这是我写的扩展。如果你想画一个圆,而不是一个椭圆,你可以写button.makeRounded(nil)
. 这里要注意的是,当你做一个圆时,垂直高度和水平高度必须相等,并且必须指定值。
extension UIButton {
func makeRounded(cornerRadius : CGFloat?) {
if let cornerRadius_ = cornerRadius {
self.layer.cornerRadius = cornerRadius_
} else {
self.layer.cornerRadius = self.layer.frame.height / 2
}
self.layer.masksToBounds = true
}
推荐阅读
- mongodb - Mongodb聚合在一个阶段检索一个字段并在另一个阶段使用它
- c++ - 有没有办法解析 CMakeFiles.txt 中的包含路径?
- python - 在 Python 中不使用 hex 函数将 Dec 转换为 Hex
- python - 根据两个数字之和从输出中删除重复项
- javascript - 名称是必需的 Nodejs API
- sql - 确保只有表中的现有用户可以在 PostgreSQL 中添加记录
- javascript - 如何为我的网页制作标题 [HTML5 , CSS3]
- javascript - 无需在 javascript 中四舍五入即可获得短关注者数量
- constants - 如何使用 laravel 将常量的值写入另一个变量
- rust - 无法推断异步方法的生命周期