Swift - Add Custom Xib View As Subview Programmatically
Asked Answered
I

1

0

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!)

but I'm getting this error

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

EDIT: 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
}

}

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
Inheritor answered 5/9, 2016 at 1:5 Comment(6)
There are lots of confusions in your question. 1) Ive made a custom xib that I've used in my storyboard before - how you made custom xib, i mean its a separate file with .xib extension, if so how you used in storyboard. i mean there is no code i can see which is using the xib's content in somewhere. 2) You are using no autolayout constraints 3) You are not initializing anything in your code form xib. I will say either edit the post and post complete code you are using for this specific implementation or some graphics that whats your goal and i can suggest best way.Audreyaudri
@MaheshAgrawala I just made an edit to the post hope that helpsInheritor
change the loadnib line like this. self.view = NSBundle.mainBundle().loadNibNamed("CardView", owner: nil, options: nil)[0] as! UIView and try onceAudreyaudri
i suggest you initialize this outside this class and add instance of this class to superview over there.Audreyaudri
@MaheshAgrawala i changed the nib load line but it crashedInheritor
try my answer below. if works otherwise i will send you a link of demo repository where you can see usage.Audreyaudri
A
0

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

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)

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.

Audreyaudri answered 5/9, 2016 at 2:29 Comment(6)
I use card view in storyboard as well wouldn't this break for those card views if i don't initialize them? (i have around 30 that i manually placed in storyboard)Inheritor
i am not getting exactly what you want to achieve. You said you used xib but now you said you are using in storyboard. and you have around 30 what? you placed 30 controller in storyboard?Audreyaudri
i think you are trying to achieve your requirement in wrong approach. If you need list with different type then you can also use table view cells of different type instead of designing 30 different type of view and using them in scroll view. or what the exact thing you want to do.Audreyaudri
sorry i wasn't very specific i have a view controller where i need to add card views programmatically and your answer would work for that alone. Using the visual storyboard interface i have also placed views with CardView as the class, I'm trying to say that i do need to programmatically add card views and setupWithSuperview() will work with those but all of the other 30 or so card views i manually added to their view controllers won't work.Inheritor
yes, if you have them in storyboard then they will not work. but how did you placed uiViews in storyboard? because in storyboard you can only place controllers right?Audreyaudri
In the view controller i added a view and the in the identity inspector of the view i set it to CardViewInheritor

© 2022 - 2024 — McMap. All rights reserved.