18
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Swift] Single View Applicationの背景色を変更してみる

Last updated at Posted at 2015-12-18
「親切すぎるiPhoneアプリ開発」page:138 参考

iPhoneアプリの概念を把握しながら背景色の変更を行ってみる

Single View Application 起動時のオブジェクト構成を把握

-> UIApplication
    -> AppDelegate
        -> UIWindow
            -> UIViewController
                -> UIView
UIApplcationとは

アプリケーション全体を管理するクラス
1アプリケーションには必ず1つのUIApplicationが存在する

AppDelegateとは

UIApplicationDelegate プロトコルを実装したクラス
アプリが起動・終了の際、 UIApplication オブジェクトが AppDelegate オブジェクトのイベントメソッドを呼び出す

UIWindowとは

最も基本的なUIView
1つのアプリケーションには1つのUIWindowがあり、その上に様々な部品を配置してアプリケーションを作成する

UIViewControllerとは

UIViewControllerは画面の遷移などを管理するクラス

UIViewとは

UIViewとは画面表示を管理するクラス
ラベルやボタンといった各種コントロールのクラスは全てこのUIViewを継承している


疑問点

UIWindow と UIView の違いは?

UIWindowクラスは、UIViewクラスの派生クラスである。
アプリ実行中、UIWindowは画面を統括管理しているため、
部分管理をUIViewに任せることになる。

「親切すぎるiPhoneアプリ開発」page:15 参考


UIViewオブジェクトの背景色を変えるには

UIViewが作成された直後に、背景色を変更するメッセージを送る

UIViewの作成タイミングを知るために

UIViewControllerの
-viewDiaLoadメソッドをオーバーライドする

viewDidLoadメソッドとは

ViewControllerによって
UIViewインスタンスが作成された直後に呼び出される

Xcode にて Single View Application のテンプレートを
選択してプロジェクトを作成している場合は、
すでに自動作成されている。
(ViewController.swift ファイル内)


ViewController.swift に以下の1行を追加する。

```swift
self.view.backgroundColor = UIColor.cyanColor()
```

```swift:ViewController.swift
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // ->背景色を空色に変更する
        self.view.backgroundColor = UIColor.cyanColor()
    }
```

Runしてみると、画面が空色に変わった。

18
18
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?