xcode 如何在情节提要上添加子视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10416530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:09:52  来源:igfitidea点击:

how to add subview over storyboard?

objective-ciosxcodestoryboardsubview

提问by iWizard

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

我有一个ViewController,我想在内容部分添加 3 个子视图。

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.

在顶部,我会放一些 3 行固定文本,然后是 tabBar(或段控件,我不知道什么是最好的),然后是子视图。

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
  1. 如何在故事板 3 个子视图中添加 IB(带有标签和文本视图)
  2. 如何在 TabBar 或段上切换它们

采纳答案by Hashmat Khalil

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

在你的头文件中创建一个 IBoutlet 并合成它。按住 conrtol 并将其拖到您的头文件中。选择 iboutlet 给它一个名字。你已准备好出发。然后使用你的插座

[self.myview addSubview:mysubview]

download the sample project

下载示例项目

回答by Palak Mistry

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.

在主故事板视图对象中添加两个按钮。将按钮文本和背景颜色设置为绿色。你可以阅读 iOS Add Click Event To UIButton Swift Example 来学习如何添加按钮点击事件功能。

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