Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift change uiviewcontroller's view

I create a new UIViewController, I don't use storyboard, this is my code. I want to change my view frame, this not work for me, I had try to add on viewWillAppear, it's still not work, I know I can add a new UIView to do it. Can I change my viewcontroller's view? Thanks your help.

import UIKit

class NewDetailViewController: UIViewController {

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

    }
    override func viewDidLoad() {
        super.viewDidLoad()

        let statusBarHeight: CGFloat = UIApplication.sharedApplication().statusBarFrame.height
        let navBarHeight             = self.navigationController?.navigationBar.frame.size.height

        println("statusBarHeight: \(statusBarHeight) navBarHeight: \(navBarHeight)")

        // view
        var x:CGFloat      = self.view.bounds.origin.x
        var y:CGFloat      = self.view.bounds.origin.y + statusBarHeight + CGFloat(navBarHeight!)
        var width:CGFloat  = self.view.bounds.width
        var height:CGFloat = self.view.bounds.height - statusBarHeight - CGFloat(navBarHeight!)
        var frame:CGRect   = CGRect(x: x, y: y, width: width, height: 100)

        println("x: \(x) y: \(y) width: \(width) height: \(height)")

        self.view.frame           = frame
        self.view.backgroundColor = UIColor.redColor()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}
like image 700
lighter Avatar asked Mar 16 '23 19:03

lighter


1 Answers

I found I add on viewDidLayoutSubviews, it work for me.

override func viewDidLayoutSubviews() {
    let statusBarHeight: CGFloat = UIApplication.sharedApplication().statusBarFrame.height
    let navBarHeight             = self.navigationController?.navigationBar.frame.size.height

    println("statusBarHeight: \(statusBarHeight) navBarHeight: \(navBarHeight)")

    // view
    var x:CGFloat      = self.view.bounds.origin.x
    var y:CGFloat      = self.view.bounds.origin.y + statusBarHeight + CGFloat(navBarHeight!)
    var width:CGFloat  = self.view.bounds.width
    var height:CGFloat = self.view.bounds.height - statusBarHeight - CGFloat(navBarHeight!)
    var frame:CGRect   = CGRect(x: x, y: y, width: width, height: height)

    println("x: \(x) y: \(y) width: \(width) height: \(height)")

    self.view.frame           = frame
    self.view.backgroundColor = UIColor.redColor()
}
like image 159
lighter Avatar answered Mar 31 '23 17:03

lighter