iOS - 如何使用来自 NIB 的特定框架初始化自定义 UIView

标签 ios swift uiview xib init

我想知道用特定框架初始化自定义 UIView 的最简洁方法是什么。

UIView 是根据 XIB 文件设计的。

这是我的实现:

class CustomView : UIView {

    @IBOutlet var outletLabel: UILabel!

    public required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupView()
    }

    public override init(frame: CGRect) {
        super.init(frame: frame)
        setupView()
    }

    private func setupView() {

        // Set text for labels
    }
}

这是我想在我的 ViewController 中初始化它的方式:

let screenSize: CGRect = UIScreen.main.bounds
let screenWidth = screenSize.width
let frame = CGRect(x: 0, y: 0, width: screenWidth - 50, height: 70)

let customView = CustomView.init(frame: frame)

但它不起作用,我有一个没有任何 socket 的白色 UIView。

如果我改为这样做:

// Extension to UIView to load Nib
let customView : CustomView = UIView.fromNib()

我可以从 XIB 文件中看到我的 view,其宽度/高度在 Interface Builder 中使用。

我想从 XIB 文件中加载具有特定框架的 view 是什么?

我是否遗漏了有关初始化的内容?

最佳答案

你可以有一个像这样的 NibLoading 类:

// NibLoadingView.swift
//source:https://gist.github.com/winkelsdorf/16c481f274134718946328b6e2c9a4d8
import UIKit

// Usage: Subclass your UIView from NibLoadView to automatically load a xib with the same name as your class

@IBDesignable
class NibLoadingView: UIView {

    @IBOutlet weak var view: UIView!

    override init(frame: CGRect) {
        super.init(frame: frame)
        nibSetup()
    }

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

    private func nibSetup() {
        backgroundColor = .clear

        view = loadViewFromNib()
        view.frame = bounds
        view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        view.translatesAutoresizingMaskIntoConstraints = true

        addSubview(view)
    }



    private func loadViewFromNib() -> UIView {
        let bundle = Bundle(for: type(of:self))
        let nib = UINib(nibName: String(describing: type(of:self)), bundle: bundle)
        let nibView = nib.instantiate(withOwner: self, options: nil).first as! UIView
        nibView.anchorAllEdgesToSuperview()
        return nibView
    }

}

extension UIView {
    func anchorAllEdgesToSuperview() {
        self.translatesAutoresizingMaskIntoConstraints = false
        if #available(iOS 9.0, *) {
            addSuperviewConstraint(constraint: topAnchor.constraint(equalTo: (superview?.topAnchor)!))
            addSuperviewConstraint(constraint: leftAnchor.constraint(equalTo: (superview?.leftAnchor)!))
            addSuperviewConstraint(constraint: bottomAnchor.constraint(equalTo: (superview?.bottomAnchor)!))
            addSuperviewConstraint(constraint: rightAnchor.constraint(equalTo: (superview?.rightAnchor)!))
        }
        else {
            for attribute : NSLayoutAttribute in [.left, .top, .right, .bottom] {
                anchorToSuperview(attribute: attribute)
            }
        }
    }

    func anchorToSuperview(attribute: NSLayoutAttribute) {
        addSuperviewConstraint(constraint: NSLayoutConstraint(item: self, attribute: attribute, relatedBy: .equal, toItem: superview, attribute: attribute, multiplier: 1.0, constant: 0.0))
    }

    func addSuperviewConstraint(constraint: NSLayoutConstraint) {
        superview?.addConstraint(constraint)
    }
}

然后您的 View 将像这样继承 NibLoadingClass:

class YourUIView: NibLoadingView {
    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

在文件所有者中设置您的 XIB 类,例如: 在这种情况下,它将是 YourUIView

enter image description here

然后实例化它:

let myView = YourUIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width-60, height: 170))

关于iOS - 如何使用来自 NIB 的特定框架初始化自定义 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48993244/

相关文章:

ios - 如何将addtarget设置为自定义UIView

iphone - 使 UIView 的特定角变圆

ios - 将 UIView 转换为自定义 View

ios - 当 Apache Cordova iOS 应用程序有新的应用程序版本时如何通知用户?

ios - react native : How to check if the user has granted permission to gps

ios - 在 Safari 上为我的 PWA 丢失 "Web Push"的解决方法

ios - 选择和取消选择 UITableViewCells - Swift

ios - 可以用两个 iOS 模拟器测试 multipeer 而在 mac 上没有设备吗?

ios - 快速检测应用程序是在后台还是在前台

ios - 删除绘图并捕获 UIView 绘图的屏幕截图无法正常工作