Swift - 以编程方式将自定义 Xib 视图添加为子视图 [英] Swift - Add Custom Xib View As Subview Programmatically

查看:42
本文介绍了Swift - 以编程方式将自定义 Xib 视图添加为子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经制作了一个我之前在故事板中使用过的自定义 xib,我只想创建一个自定义视图的实例调整大小,然后将其作为子视图添加到 uiscrollview.我试过在我的视图控制器的 viewdidload func 中使用这段代码

Ive made a custom xib that I've used in my storyboard before and i want simply create an instance of the custom view adjust size and then add it as a subview to a uiscrollview. Ive tried using this block of code in the viewdidload func of my view controller

let cardView = CardView(coder: NSCoder())
cardView!.frame.size.width = 100
cardView!.frame.size.height = 100
scrollView.addSubview(cardView!)

但我收到此错误

Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '*** -containsValueForKey: cannot be sent to an abstract object
of class NSCoder: Create a concrete instance!'

这是连接到 CardView.xib 的 swift 文件的代码

this is the code for the swift file connected to CardView.xib

import UIKit

class CardView: UIView {
@IBOutlet var view: UIView!
@IBOutlet weak var cornerView: UIView!

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

    NSBundle.mainBundle().loadNibNamed("CardView", owner: self, options: nil)
    self.addSubview(view)
    view.frame = self.bounds

    cornerView.layer.cornerRadius = 3
    cornerView.layer.masksToBounds = true

    view.layer.shadowOffset = CGSizeMake(1, 5);
    view.layer.shadowRadius = 2;
    view.layer.shadowOpacity = 0.2;
    view.layer.masksToBounds = false
}

}

我没有使用自动布局,而是尝试简单地设置高度和宽度来测试从这两行手动添加子视图(也只是提醒一下,我是 iOS 开发的新手)

instead of using auto layout i tried simply settings height and width to test adding subviews manually from these 2 lines(also just a heads up i am new to iOS development)

cardView!.frame.size.width = 100
cardView!.frame.size.height = 100

推荐答案

我在使用自定义 XIB 进行视图初始化的情况下使用的内容如下.

What i have used in case of using custom XIB for view initialization is below.

在像你的 CardView 这样的视图类中,代码如下.

In the class of the view like for you its CardView the code goes like.

class CardView: UIView {
    @IBOutlet weak var cornerView: UIView!

    func setupWithSuperView(superView: UIView) {
        self.frame.size.width = 100
        self.frame.size.height = 100
        superView.addSubview(self)

        cornerView = UIView(frame: self.bounds)
        cornerView.layer.cornerRadius = 3
        cornerView.layer.masksToBounds = true

        view.layer.shadowOffset = CGSizeMake(1, 5);
        view.layer.shadowRadius = 2;
        view.layer.shadowOpacity = 0.2;
        view.layer.masksToBounds = false
    }
}

在你调用这个类进行初始化的地方,使用这个.

and where you are calling this class for initialization, use this.

let cardView = NSBundle.mainBundle("CardView").loadNibNamed("", owner: nil, options: nil)[0] as! CardView
cardView.setupWithSuperView(scrollView)

试试这个.但请确保 xib 文件的第一个视图是 CardView 类型.我的意思是第一个视图的类是 CardView.

Try this once. But make sure the first view of the xib file is of type CardView. I mean the class of the first view is CardView.

这篇关于Swift - 以编程方式将自定义 Xib 视图添加为子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆