gpt4 book ai didi

ios - 快速改变 uiviewcontroller 的 View

转载 作者:搜寻专家 更新时间:2023-10-31 08:23:38 24 4
gpt4 key购买 nike

我创建了一个新的 UIViewController,我不使用 Storyboard,这是我的代码。我想改变我的 View 框架,这对我不起作用,我尝试添加 viewWillAppear,它仍然不起作用,我知道我可以添加一个新的 UIView 到做吧。我可以更改 View Controller 的 View 吗?感谢您的帮助。

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()
}
}

最佳答案

我发现我添加了 viewDidLayoutSubviews,它对我有用。

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()
}

关于ios - 快速改变 uiviewcontroller 的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30683121/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com