简体   繁体   中英

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:

上面问题的一个例子

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

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

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
       }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM