Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

satyadevchauhan/CustomView

Open more actions menu

Repository files navigation

CustomView

Create a class CustomView.swift and xib named CustomView.xib.

Link File Owner

  • Open CustomView.xib
  • Click on file owner and provide the custom class name.

LinkFileOwner

Link Root View

  • Open CustomView.xib
  • Click root view and join the view outlet with file owner.

Link Root View

Load view from XIB in code

  • Open CustomView.swift
  • Add following code to load view from xib. Here xib name is same as class name.
func loadViewFromNib() {
        let bundle = Bundle(for: type(of: self))
        let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil).first as! UIView
        addSubview(view)
        view.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            self.topAnchor.constraint(equalTo: view.topAnchor),
            self.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            self.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            self.bottomAnchor.constraint(equalTo: view.bottomAnchor),
        ])
        
        self.view = view
    }

Invoke loadViewFromNib() from Init

  • Call the loadViewFromNib() from following init methods.
override init(frame: CGRect)
required public init?(coder aDecoder: NSCoder)

Embed CustomView

  • Open your storyboard or viewcontroller xib.
  • Drag UIView and add appropriate constraints.
  • Change class for this view to CustomView.
  • Create outlet for to use in code.

EmbedCustomView

About

CustomView with XIB

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.