UIButton未添加到子视图中

时间:2019-05-08 11:34:19

标签: swift

我创建了一个UIButton并将其添加到我的视图控制器中,但是在加载时,即使将其添加到子视图和约束中,按钮也不会显示

var plusBtn: DefaultBtn = {
        let plusBtn = DefaultBtn(type: .custom)
        plusBtn.translatesAutoresizingMaskIntoConstraints = false
        return plusBtn
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        view.addSubview(plusBtn)
        plusBtn.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20).isActive = true
        plusBtn.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20).isActive = true
        plusBtn.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20).isActive = true
        plusBtn.setTitle("BUT WHY", for: .normal)
    }

class DefaultBtn: UIButton {

    override func awakeFromNib() {
//        layer.cornerRadius = self.frame.size.height / 2
        layer.borderWidth = 1
        layer.borderColor = UIColor.black.cgColor
        backgroundColor = .white
//        heightAnchor.constraint(equalToConstant: 50).isActive = true
//        widthAnchor.constraint(equalToConstant: 50).isActive = true
        translatesAutoresizingMaskIntoConstraints = false
        setTitleColor(.black, for: .normal)

    }
}

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

尝试

const fs = require('fs')
const path = require('path')

let dir = 'C:/'
function customRecursiveWalker(dir) {
  fs.readdirSync(dir).forEach(file => {
    let fullPath = path.join(dir, file)
    // Folders
    if (fs.lstatSync(fullPath).isDirectory()) {
      fs.appendFileSync("C:/Users/USERNAME/Desktop/paths.txt", `${fullPath}\n`)
      customRecursiveWalker(fullPath)
    } 
    // Files
    else {
      fs.appendFileSync("C:/Users/USERNAME/Desktop/paths.txt", `${fullPath}\n`)
    }  
  })
}
customRecursiveWalker(dir)

OR

 view.backgroundColor = .red

按钮的默认标题颜色为白色,与视图的背景色相同


plusBtn.setTitleColor(.red, for: .normal)