Skip to content

Instantly share code, notes, and snippets.

@el-hoshino
Last active March 4, 2018 04:44
Show Gist options
  • Save el-hoshino/05865ae93bf66ccd7032 to your computer and use it in GitHub Desktop.
Save el-hoshino/05865ae93bf66ccd7032 to your computer and use it in GitHub Desktop.
Storyboard 抜きで、コードオンリーで iOS アプリの UI を作る ref: https://qiita.com/lovee/items/5e8c7752d7e383660543
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// アプリウィンドウを設定します。
self.window = UIWindow(frame: UIScreen.main.bounds)
// ウィンドウをヴィジブルにします。
self.window?.makeKeyAndVisible()
// ウィンドウの rootViewController を viewController に設定します。
self.window?.rootViewController = viewController
return true
}
override func layoutSubviews() {
// 必ず `super` の `layoutSubviews()` を呼び出します
super.layoutSubviews()
// `mainLabel` がほしいサイズを自分のサイズから `sizeThatFits(_:)` を通して取り出します
let labelSize = self.mainLabel.sizeThatFits(self.bounds.size)
// `mainLabel` を真ん中に置くように、原点座標を先ほど取得したサイズと自分のサイズから割り出します
let x = (self.bounds.width - labelSize.width) / 2
let y = (self.bounds.height - labelSize.height) / 2
let labelOrigin = CGPoint(x: x, y: y)
// `mainLabel` のレイアウトは、`frame` に原点座標とサイズで代入して決めます
self.mainLabel.frame = CGRect(origin: labelOrigin, size: labelSize)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// 画面サイズを初期値として `MainView` クラスを `mainView` としてインスタンス化します。
let mainView = MainView(frame: self.view.bounds)
// `MainView` に自動サイズ調整用に `autoresizingMask` を設定
mainView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
// `mainView` オブジェクトを表示します。
self.view.addSubview(mainView)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment