環境
MaxOS: Sierra 10.12.6
Xcode: 8.3.3
やりたい事
storyboardなしでラベルを表示させたい。
やった事
以下のコードを書きました。
swift
1import UIKit 2 3@UIApplicationMain 4class AppDelegate: UIResponder, UIApplicationDelegate { 5 6 var window: UIWindow? 7 8 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 9 10 window = UIWindow(frame: UIScreen.main.bounds) 11 window?.backgroundColor = UIColor.white 12 window?.rootViewController = ViewController() 13 window?.makeKeyAndVisible() 14 15 return true 16 } 17} 18 19class ViewController: UIViewController { 20 21 override func viewDidLoad() { 22 super.viewDidLoad() 23 24 let label = UILabel() 25 label.text = "test" 26 label.textColor = UIColor.black 27 label.center = self.view.center 28 self.view.subviews.first?.addSubview(label) 29 } 30}
ViewControllerに他にaddSubviewはしてないので
subviews.first
で行けると思ったのですが。。。
困っていること
Runは出来ていてエラーもないのですが、
真っ白な画面が出るだけでlabelが表示されません。
何故でしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/09 00:47
2017/09/09 01:05
2017/09/09 01:06
2017/09/09 01:07