簡體   English   中英

如何在情節提要上添加子視圖?

[英]how to add subview over storyboard?

我有一個ViewController ,我想在內容部分添加 3 個子視圖。

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

我想創建一些看起來像這個圖像示例的東西:

上面問題的一個例子

具體來說,我的問題是:

  1. 如何在故事板 3 個子視圖中添加 IB(帶有標簽和文本視圖)
  2. 如何在 TabBar 或段上切換它們

在你的頭文件中創建一個 IBoutlet 並合成它。 按住 conrtol 並將其拖到您的頭文件中。 選擇 iboutlet 給它一個名字。 你已准備好出發。 然后使用你的插座

[self.myview addSubview:mysubview]

下載示例項目

在主故事板視圖對象中添加兩個按鈕。 將按鈕文本和背景顏色設置為綠色。 你可以閱讀 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
       }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM