首页 > 解决方案 > 在 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”,因为无法找到其可执行文件。

图像

标签: swiftxcode

解决方案


这是我写的扩展。如果你想画一个圆,而不是一个椭圆,你可以写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
    }

推荐阅读