iWizard
iWizard

Reputation: 7094

how to add subview over storyboard?

I have a ViewController and I want to add 3 subviews in the content part.

On the top i would put some 3-line fixed text and then tabBar (or segment control, I don't know what is best) and then subview.

I want to create something such that it looks like this image example:

An example of the above problem

Specifically, my questions are:

  1. How to add over IB in storyboard 3 subviews (with labels and textViews)
  2. How to switch them over TabBar or segment

Upvotes: 2

Views: 7199

Answers (2)

Palak Mistry
Palak Mistry

Reputation: 13

Add two button in the main storyboard view object. Set button text, and background color to green. You can read iOS Add Click Event To UIButton Swift Example to learn how to add button click event function.

import UIKit
class ViewController: UIViewController {
       // Create the subview object.
       private let childView = UIView();
       // When click the second button will invoke this method.
       @IBAction func removeSubView(_ sender: UIButton, forEvent event: UIEvent) {
             // Remove the child view.
             childView.removeFromSuperview();
       }
       // Invoked when click the first button.
       @IBAction func addSubView(_ sender: UIButton, forEvent event: UIEvent) {
             // Add the child view.
             self.view.addSubview(childView);
       }        
       override func viewDidLoad() {
             super.viewDidLoad()
             // Set child view x y cordinate and size.
             childView.frame = CGRect(x: 80, y: 100, width: 200, height: 100)
             // Set child view background color.
             childView.backgroundColor = UIColor.green
       }
}

Upvotes: 0

Hashmat Khalil
Hashmat Khalil

Reputation: 1816

create an IBoutlet in your header file and synthesize it. hold conrtol and drag it to your header file. choose iboutlet give it a name. you are good to go. then use your outlet

[self.myview addSubview:mysubview]

download the sample project

Upvotes: 1

Related Questions