Add Subview and Remove subview example in Swift

In this video I am going to show you how to add custom user interface as a subview and how to remove it from super view on a button tap event.

Also, in this video I am using the project I have created earlier. You can watch video tutorial and learn how I’ve created Custom User Interface in XIB file here: Creating Custom User Interface with XIB in Xcode 6 and Swift

Source code created during the video


import UIKit
class ViewController: UIViewController {

var mySubview:MyCustomView!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBAction func showSubviewButtonTapped(sender: AnyObject) {

if(mySubview != nil && !mySubview.view.hidden)
{
mySubview.view.removeFromSuperview()
}

mySubview = MyCustomView(frame: CGRect(x:10,y:300, width:275, height:147))

mySubview.titleLabel.text = “My custom label text”
mySubview.myButton.setTitle(“Hide”, forState: UIControlState.Normal)
mySubview.myButton.addTarget(self, action: “myHideButtonTapped:”, forControlEvents: UIControlEvents.TouchUpInside)

let catImage = UIImage(named: “cat1.jpeg”)
mySubview.myImage.image = catImage

self.view.addSubview(mySubview)

}

func myHideButtonTapped(sender:UIButton)
{
mySubview.view.removeFromSuperview()
}

}


Learn iOS Development with these Video Courses

The Complete iOS 11 & Swift Developer Course - Build 20 Apps

Use Xcode 9 & Swift 4 to make real apps like Uber and Instagram, with CoreML & ARKit. Includes AWS Credit and much more. Preview this video course. icon

How to Make a Freaking iPhone App - iOS 11 and Swift 4

iPhone App Development from scratch. Learn how to make iOS apps using Xcode 9. The Basics Include Pokemon Go & Snapchat. Learn to build iOS Apps with Xcode 9 and Swift 4. How to Make a Freaking iPhone App - iOS 11 and Swift 4 icon

iOS 11 and Xcode 9 - Complete Swift 4 & Objective-C Course

A Complete iOS 11 and Xcode 9 Course with Swift 4 & Objective-C. Preview this video course. iOS 11 and Xcode 9 - Complete Swift 4 & Objective-C Course icon
  • Vince

    Hi, I am researching about adding a UISegmentedControl inside UITableViewHeaderFooterView. I am hoping your tutorial will be the final puzzle. Currently, I have been able to add a UILabel without using a Subview. The problem is I just can’t add a UISegmentedControl. So I am thinking I need to create a custom Subview and include a UISegmentedControl in it, and then add the Subview into the UITableViewHeaderFooterView. 🙂 I hope that will work.

  • Vince

    I am stuck on the MyCustomView.swift file error–pls see attached screenshot. And if I change it to “as!”, the error disappears but then I get two same errors on the two super.init() lines. This is the error: “Property ‘self.view’ not initialised at super.init call. Any tips?

    • Hmmm…

      let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
      return view

      Works well for me…

  • Alex

    I have a EXC_BAD_ACCESS when: let nib = UINib(nibName: “CustomView”, bundle: bundle) called