我使用以下代码:
override func viewDidLoad() {
super.viewDidLoad()
let testView = UIView(frame: CGRectMake(0, 0, 320, 568))
testView.backgroundColor = UIColor.redColor()
self.view.addSubview(testView)
}
当视图加载时不会发生。
但是在IBAction工作很好
为什么?
答案 0 :(得分:0)
首先尝试使用swift语法CGRect(x:0,y:0,width:320,height:568)并向我们展示创建ViewController的方式。
确保调用viewDidLoad()方法,因为此代码适用于我。
<强> AppDelegate.swift 强>
var window: UIWindow?
let viewController = ViewController()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let tabBarController = UITabBarController()
let controllers = [self.viewController]
tabBarController.viewControllers = controllers
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window!.backgroundColor = UIColor.whiteColor()
self.window!.rootViewController = tabBarController
self.window!.makeKeyAndVisible()
return true
}
<强> ViewController.swift 强>
override func viewDidLoad() {
super.viewDidLoad()
let testView = UIView(frame: CGRect(x:0, y:0, width:320, height:568))
testView.backgroundColor = UIColor.redColor()
self.view.addSubview(testView)
}