[英]How to add an UIView as a subview to Storyboard's view without any delay?
[英]how to add subview over storyboard?
在你的頭文件中創建一個 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.