iOS Basically: Reusable UIView - Programming in Swift (Part 1)

Swiftly · 29K views · 2014 · 12:23

Open on YouTube

Software (other)UX & UI designtutorialdated

Why Stack Overflow recommends it

Stack Overflow users link it when answering how to create a custom UIView from a XIB file and reuse it in storyboards or add it programmatically.

Recommended by 4 people across 4 different months, 2015 to 2017.

What it is

A short iOS tutorial from the Swiftly channel on building a reusable UIView in Swift. It pairs a UIView subclass with a .xib file, using a custom keyboard as the example, so the same component can be dropped into storyboards across an app.

What people said

Recommends Pvt. Joker · 2015-05 · Stack Overflow · on “Real popover view programmatically on iPhone”

After hours of trying I got it working. Together with this [this video] tutorial video which shows how to use views from XIB files and the comment from @matt I achieved to show popups from UIControls. To instantiate my XIB file I created a UIView sublcass like this: import UIKit class LinkAreaPopupView: UIView { // outlets @IBOutlet var view: UIView! @IBOutlet weak var btnDeleteArea: UIButton! @IBOutlet weak var btnLinkTo: UIButton! required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder) NSBundle.mainBundle().loadNibNamed("LinkAreaPopupView", owner: self, options: nil) self.addSubview(self.view) } override init(frame: CGRect) { super.init(frame: frame) NSBundle.mainBundle().loadNibNa

Recommends ded · 2015-07 · Stack Overflow · on “Swift: Append Custom UIView programmatically”

This is a near duplicate of this other SO question [LINK] , however I'm running into some issues that it makes it appear like I'm not doing it correctly. For reference, I followed this excellent tutorial on creating re-usable views [this video] from a custom class and xib file (it's only a few minutes :) and I have no problems at all dragging that into another view onto my storyboard (as demonstrated at the end of the video) Nevertheless for my case — I'm trying to call my custom class programmatically, and add it as a subview to one of my ScrollView instances. class MainController: UIViewController { @IBOutlet weak var scrollView: UIScrollView! override func viewDidLoad() { super.viewDidLoad() }

Recommends Suragch · 2015-12 · Stack Overflow · on “How do I create a custom iOS view class and instantiate multiple copies of it (in IB)?”

Swift example Updated for Xcode 10 and Swift 4 (and reportedly [LINK] still works for Xcode 12.4/Swift 5) Here is a basic walk through. I originally learned a lot of this from watching this Youtube video series [this video] . Later I updated my answer based on this article [LINK] . Add custom view files The following two files will form your custom view: .xib file to contain the layout .swift file as UIView subclass The details for adding them are below. Xib file Add a .xib file to your project (File > New > File... > User Interface > View) . I am calling mine ReusableCustomView.xib . Create the layout that you want your custom view to have. As an example, I will make a layout with a UILabel and

Recommends ekscrypto · 2017-09 · Stack Overflow · on “How to implement class swizzling swift?”

I believe what you are trying to do is create a re-usable UIView with your control buttons, that you can use in different view controllers. There is a great tutorial on Youtube entitled iOS Basically: Reusable UIView - Programming in Swift (Part 1) [this video] . Basically, you will want to create a .Xib file dedicated to the view that you want to re-use, with all the actions handled by that custom view class. Every time you will want your custom view, you will need to instantiate it from the Xib file and manually add it as a subview onto the container view. Good luck and happy coding! -- edit -- Your updated code shows that this will be part of a UITableView and you are trying to add your custom

Cites Uzumaki Naruto · 2015-08 · Stack Overflow · on “Swift: Reusable UIView in storyboard and sizing constraints”

I'm trying to create a reusable UIView in Swift that I can plug into my Storyboard view controllers. My key issue right now is that the reusable UIView "widget" doesn't fully fit into the UIView box in the storyboard. I followed this tutorial [this video] to set up the reusable UIView widget Created a subclass of UIView and a corresponding .xib -- and connected these: import UIKit class MyWidgetView: UIView { @IBOutlet var view: UIView!; required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder); NSBundle.mainBundle().loadNibNamed("MyWidgetView", owner: self, options: nil); self.addSubview(self.view); } } In the XIB, which is the interface file corresponding to the code above, I used U

Cites Suragch · 2015-12 · Stack Overflow · on “How to properly subclass a view class which is loaded from xib in Swift?”

Make sure you make the set the File's Owner for the .xib file to the .swift file. Also add an outlet for the root view and then load the xib from code. This is how I did it for a similar project: import UIKit class ResuableCustomView: UIView { @IBOutlet var view: UIView! @IBOutlet weak var label: UILabel! @IBAction func buttonTap(sender: UIButton) { label.text = "Hi" } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) NSBundle.mainBundle().loadNibNamed("ReusableCustomView", owner: self, options: nil)[0] as! UIView self.addSubview(view) view.frame = self.bounds } } It is a little different than yours but you can add the init:frame method. If you have an awakeFromNib method