63

I am trying to access the MyCustomView from another class using the following code in ViewController.swift ..

var view = MyCustomView(frame: CGRectZero)

.. in the viewDidLoad method. The problem is the view does not get initialized in the simulator.

I have already set class in storyboard for the current ViewController.

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)
    }
Stefan Arentz
  • 34,311
  • 8
  • 67
  • 88
LC 웃
  • 18,888
  • 9
  • 57
  • 72

5 Answers5

66

The CGRectZero constant is equal to a rectangle at position (0,0) with zero width and height. This is fine to use, and actually preferred, if you use AutoLayout, since AutoLayout will then properly place the view.

But, I expect you do not use AutoLayout. So the most simple solution is to specify the size of the custom view by providing a frame explicitly:

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

Note that you also need to use addSubview otherwise your view is not added to the view hierarchy.

RestingRobot
  • 2,938
  • 1
  • 23
  • 36
Stefan Arentz
  • 34,311
  • 8
  • 67
  • 88
  • thanks stefan...but saying CGRectZero and passing values does make anysense? – LC 웃 Mar 13 '15 at 14:08
  • 1
    it makes sense if you use Auto Layout. Because in that case the actual placement and size of your view is determined by the Auto Layout Constraints and not by what you pass to the `init(frame:)` initializer. – Stefan Arentz Mar 13 '15 at 14:14
  • 1
    Should that line read `customView = MyCustomView(frame: CGRect(top: 0, left: 0, width: 200, height: 50))` rather than `customView = MyCustomView(frame: CGRectZero(top: 0, left: 0, width: 200, height: 50)` – dumbledad Feb 28 '16 at 21:47
  • If I have any constraints ( e.g. horizontal alignment X) added for customView, then there is a good change that they will peacefully override the frame of customView? Right? – mfaani Aug 02 '16 at 18:18
36

Swift 3 / Swift 4 Update:

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)
David Seek
  • 16,783
  • 19
  • 105
  • 136
  • Change `let screenSize: CGRect = UIScreen.main.bounds` to `let screenSize: CGRect = UIScreen.main.bounds.size` – Tkas Aug 25 '19 at 10:04
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)
       }
Robert
  • 5,278
  • 43
  • 65
  • 115
Sateesh Pasala
  • 772
  • 8
  • 15
5
let viewDemo = UIView()
viewDemo.frame = CGRect(x: 50, y: 50, width: 50, height: 50)
self.view.addSubview(viewDemo)
Pankaj Jangid
  • 812
  • 9
  • 18
1
view = MyCustomView(frame: CGRectZero)

In this line you are trying to set empty rect for your custom view. That's why you cant see your view in simulator.

Pavel Gatilov
  • 2,570
  • 2
  • 18
  • 31