如何在Swift中以编程方式创建具有控件文本框、按钮等的自定义视图

63

我正在尝试使用 ViewController.swift 中的以下代码从另一个类访问 MyCustomView ..

var view = MyCustomView(frame: CGRectZero)

问题在于模拟器中视图没有初始化,这是因为它没有在viewDidLoad方法中调用。

我已经在Storyboard中为当前的视图控制器设置了类。

class MyCustomView: UIView {
    var label: UILabel = UILabel()
    var myNames = ["dipen","laxu","anis","aakash","santosh","raaa","ggdds","house"]

    override init(){
        super.init()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addCustomView()
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func addCustomView() {
        label.frame = CGRectMake(50, 10, 200, 100)
        label.backgroundColor=UIColor.whiteColor()
        label.textAlignment = NSTextAlignment.Center
        label.text = "test label"
        label.hidden=true
        self.addSubview(label)

        var btn: UIButton = UIButton()
        btn.frame=CGRectMake(50, 120, 200, 100)
        btn.backgroundColor=UIColor.redColor()
        btn.setTitle("button", forState: UIControlState.Normal)
        btn.addTarget(self, action: "changeLabel", forControlEvents: UIControlEvents.TouchUpInside)
        self.addSubview(btn)

        var txtField : UITextField = UITextField()
        txtField.frame = CGRectMake(50, 250, 100,50)
        txtField.backgroundColor = UIColor.grayColor()
        self.addSubview(txtField)
    }

2
你在初始化时是否调用了self.view.addSubview(myCustomView)? - IxPaka
5个回答

66

CGRectZero常量等同于一个位于(0,0)位置,宽度和高度均为零的矩形。如果您使用AutoLayout,则可以使用它,并且实际上更受欢迎,因为AutoLayout将正确地放置视图。

但是,我预计您不会使用AutoLayout。因此,最简单的解决方案是通过显式提供框架来指定自定义视图的大小:

customView = MyCustomView(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
self.view.addSubview(customView)

请注意,您还需要使用addSubview,否则您的视图将不会添加到视图层次结构中。


谢谢Stefan...但是说CGRectZero并传递值没有任何意义吧? - LC 웃
1
如果您使用自动布局,那么这将是有意义的。因为在这种情况下,视图的实际位置和大小是由自动布局约束确定的,而不是由传递给 init(frame:) 初始化程序的内容确定。 - Stefan Arentz
1
那一行应该写成 customView = MyCustomView(frame: CGRect(top: 0, left: 0, width: 200, height: 50)) 而不是 customView = MyCustomView(frame: CGRectZero(top: 0, left: 0, width: 200, height: 50) - dumbledad
如果我为customView添加了任何约束(例如水平对齐X),那么它们很可能会平稳地覆盖customView的框架,对吗? - mfaani

36

Swift 3 / Swift 4 更新:

let screenSize: CGRect = UIScreen.main.bounds
let myView = UIView(frame: CGRect(x: 0, y: 0, width: screenSize.width - 10, height: 10))
self.view.addSubview(myView)

let screenSize: CGRect = UIScreen.main.bounds 更改为 let screenSize: CGRect = UIScreen.main.bounds.size - Tkas

10
var customView = UIView()


@IBAction func drawView(_ sender: AnyObject) {

    customView.frame = CGRect.init(x: 0, y: 0, width: 100, height: 200)
    customView.backgroundColor = UIColor.black     //give color to the view 
    customView.center = self.view.center  
    self.view.addSubview(customView)
       }

5
let viewDemo = UIView()
viewDemo.frame = CGRect(x: 50, y: 50, width: 50, height: 50)
self.view.addSubview(viewDemo)

1
view = MyCustomView(frame: CGRectZero)

在这行代码中,你试图为自定义视图设置一个空的矩形。这就是为什么你无法在模拟器中看到你的视图的原因。

我也尝试过这种方式 var view = MyCustomView(frame: CGRectMake(10, 40, 500, 500)) - LC 웃
你需要在控制器的viewDidLoad或其他地方调用addSubview方法。例如:self.view.addSubview(yourView)。 - Pavel Gatilov
它可能是其他任何东西。比如你标记了某个地方的userInteractionEnabled或者其他什么... - Pavel Gatilov
我刚发现如果我调用 MyCustomView(frame: CGRectZero),那么视图会被冻结... :) - LC 웃

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接