0

I'm trying to use a custom uiview that is in a xib file. I'm a beginner and don't know where to start. I have this so far, but I don't know what I should set my placeholder view equal to

edit: full viewController:

import UIKit

class Viewcontroller: UIViewController {

    
    @IBOutlet weak var placeholderView: UIView!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        placeholderView = view
        
    }
    
    class func instanceFromNib() -> CustomView {
            let view = UINib(nibName: "CustomView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! CustomView
            return view
    }
    
9
  • "to use a custom uiview that is in a xib file" doesn't tell the whole story. Where and how is that UIView object going to be used? Commented Dec 17, 2020 at 5:37
  • I currently have a uiview inside a uiviewcontroller that’s a placeholder. I’m trying to replace the placeholder uiview with different customViews. I’m trying to start with this first customView but it doesn’t change when I set placeholderView = view Commented Dec 17, 2020 at 5:39
  • And where do those four lines of code come from? Unless you specify the class name, others won't be able tell where you are writing code. Commented Dec 17, 2020 at 5:42
  • I posted the whole viewController now Commented Dec 17, 2020 at 5:46
  • placeholderView = view doesnt do anything Commented Dec 17, 2020 at 5:46

1 Answer 1

1

A. Let me suppose that you have a xib file titled 'TestView.' Also let me suppose that you have a subclass file of UIView titled 'TestView.' B. Select your xib file. And select File's Owner. And set the class to TestView (the name of the subclass file). C. Open the xib file with Interface Builder. Select 'Custom View.' (in the middle pane) IBOutlet-Connect this view to your UIView swift file. For example, name it like the following. @IBOutlet var customView: NSView! D. This subclass file should have the following.

import UIKit

class TestView: UIView {
    @IBOutlet var contentView: UIView!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }
    
    private func commonInit() {
        Bundle.main.loadNibNamed("TestView", owner: self, options: nil)
        addSubview(contentView)
        contentView.frame = self.bounds
    }
}

E. Open the storyboard. Drag and drop a Custom View onto the view controller scene. Under the identity inspect, set the class name to TestView. F. IBOutlet-connect the custom view object in the view controller like the following.

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var testView: TestView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

That's all.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry for not being specific, but I eventually am going to need to change the placeholderView to multiple different customViews so I can’t just go into the storyboard and change the class to one customView

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.