Search code examples
iosuiviewswift2xcode7-beta5

Getting View Size Not Getting Resized Size


In the view controller class I'm trying to make buttons programmatically in a 10x10 grid. I have constraints and the view is resizing properly, but the size I'm getting back and seeing from the print statement is the original size from the story board and not the resized size. How do I get the new size?

func buttonGridder() {
    for x in 0..<10 {
        for y in 0..<10{
            let sizer = ButtonGrid.frame.width
            let buttonSize:CGFloat = ButtonGrid.frame.width / CGFloat(10)
            print("\(x), \(y), \(buttonSize), \(sizer)")
            let letterButton = WordButton(column: x, row: y, buttonSize: buttonSize, buttonMargin: 0)
            self.ButtonGrid.addSubview(letterButton)
        }
    }
}

Solution

  • Call the Log function in this method viewDidLayoutSubviews. This is the method that is called after views are positioned accordingly to the constraints after viewDidAppear. Note : This method is called multiple times in a single go, so DO NOT alloc or add in this method.

    AND if you are adding or changing the view size later on then call setNeedsDisplay to redraw all the Views inside the View controller