以Xib作为根视图的自定义视图

时间:2019-09-04 18:25:33

标签: ios swift xib nib

创建自定义视图时,可以将根视图设置为xib吗?

当前,如果创建自定义视图,则需要将笔尖添加为子视图。

如果要更改背景颜色,则不能再使用UIView API。

async function uploadImage() {
    var row   = document.getElementsByClassName('row');
    var file  = document.getElementsByClassName('img-file');
    var name  = document.getElementsByClassName('img-name');

    for (let i=0; i<row.length; i++) {
      var image = file[i].files[0];
      if (image) {
        var reader = new FileReader();
        reader.readAsDataURL(image);
        let promiseOfAllDone = new Promise(res=>reader.addEventListener('loadend',res))
            .then(event=>new Promise((resolve, reject) => { 
                    google.script.run.withSuccessHandler(resolve)
                                 .uploadImgToDrive(name[i].value, event.target.result)
             }).then(() => row[i].innerHTML='')
            .catch(e=>console.error(e));
        await promiseOfAllDone;//wait for all promises to be fulfilled
       }
    }
}

所以,有可能做到这一点。

customView.backgroundColor = UIColor.red // Won't work because nib view is covering the "root" view.

1 个答案:

答案 0 :(得分:0)

使用awakeAfter:

open override func awakeAfter(using _: NSCoder) -> Any? {
    // set the tag in the view's XIB, so we don't create an infinite loop
    guard tag != 999 else {
        return self
    }

    let view = type(of: self).instantiate() // Convenience method that chooses nib based on class name
    view.frame = frame
    view.autoresizingMask = autoresizingMask
    view.translatesAutoresizingMaskIntoConstraints = translatesAutoresizingMaskIntoConstraints
    view.tag = tag
    for constraint in constraints {
        let firstItem = constraint.firstItem as? UIView == self ? view : constraint.firstItem
        let secondItem = constraint.secondItem as? UIView == self ? view : constraint.secondItem
        let constraintToAdd = NSLayoutConstraint(item: firstItem as Any, attribute: constraint.firstAttribute, relatedBy: constraint.relation, toItem: secondItem, attribute: constraint.secondAttribute, multiplier: constraint.multiplier, constant: constraint.constant)
        constraintToAdd.priority = constraint.priority
        view.addConstraint(constraintToAdd)
    }

    return view
}