首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用xibs嵌套@IBDesignable?

使用xibs嵌套@IBDesignable?
EN

Stack Overflow用户
提问于 2017-11-29 12:05:03
回答 1查看 162关注 0票数 0

我试图在iOS上创建一个基于组件的系统,我想做以下工作:

  1. 创建一个"PaddedView“组件,它在任何添加的子组件周围都有8px的空间,比如容器类型组件。
  2. 将另一个IBDesignable视图添加到故事板上的PaddedView中,并查看两个呈现。

这个是可能的吗?

现在,对于所有IBDesignable组件,我使用以下超类从xibs加载它们的视图:

代码语言:javascript
复制
import Foundation
import UIKit

@IBDesignable
class SKDesignableView: UIView {
    var view: UIView?

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.loadXib()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.loadXib()
    }

    func loadXib() {
        self.view = self.viewFromXib()
        self.view!.frame = self.bounds
        self.view!.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
        self.addSubview(self.view!)
    }

    func viewFromXib() -> UIView {
        let bundle = UINib(nibName: String(describing: self.getType()), bundle: Bundle(for: self.getType()))
        let views = bundle.instantiate(withOwner: self, options: nil)
        return views.first as! UIView
    }

    func getType() -> AnyClass {
        fatalError()
    }
}

如何为其他IBDesignables创建占位符?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-05 00:41:12

视图最初包含子视图,因此将容器视图作为子视图添加到任何需要子组件的组件中。

代码语言:javascript
复制
func loadXib() {
        var subview: UIView? = nil
        if self.subviews.count > 0 {
            subview = self.subviews[0]
        }
        subview?.removeFromSuperview()

        self.view = self.viewFromXib()
        self.view!.frame = self.bounds
        self.view!.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]

        if let subview = subview {
            let lConstraint = NSLayoutConstraint(item: subview, attribute: NSLayoutAttribute.leading, relatedBy: NSLayoutRelation.equal,
                    toItem: self.view!, attribute: NSLayoutAttribute.leading, multiplier: 1, constant: 8)
            let rConstraint = NSLayoutConstraint(item: subview, attribute: NSLayoutAttribute.trailing, relatedBy: NSLayoutRelation.equal,
                    toItem: self.view!, attribute: NSLayoutAttribute.trailing, multiplier: 1, constant: -8)
            let tConstraint = NSLayoutConstraint(item: subview, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal,
                    toItem: self.view!, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 8)
            let bConstraint = NSLayoutConstraint(item: subview, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal,
                    toItem: self.view!, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: -8)
            self.view!.addSubview(subview)
            self.view!.addConstraints([lConstraint, rConstraint, tConstraint, bConstraint])
        }
        self.addSubview(self.view!)
    }

这种方法可以推广到标签等,以添加多个子视图。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47552442

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档