0
0

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 1 year has passed since last update.

Swift Playgrounds メモ

Posted at

エラー内容が表示されない問題

以下のコードを先頭に入れておく

NSSetUncaughtExceptionHandler { exception in
    print("Exception thrown: \(exception)")
    exception.callStackSymbols.forEach{print($0)}
}

UINanavigationViewController  の navigationbar が上部にめり込む問題

これはよくわからなかった・・。
とりあえず、以下のコードのように UIViewController でラップして解決するしかなさそう。

class SecondViewController: UIViewController {}

class RootViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "タイトル"

        navigationController?.pushViewController(SecondViewController(), animated: true)
    }
}

let rootViewController = RootViewController()
let navigationController = UINavigationController(rootViewController: rootViewController)


let wrapperViewController = UIViewController()
wrapperViewController.addChild(navigationController)
wrapperViewController.view.addSubview(navigationController.view)
PlaygroundPage.current.liveView = wrapperViewController
PlaygroundPage.current.needsIndefiniteExecution = true

ローカルの画像を読み込みたい

左メニューの resources ディレクトリに画像をドラッグドロップして以下のような形で利用するだけ。

let myImage = UIImage(named: "myImageFileName")

AppDelegate に書いていた UIBarButtonItem.appearance() などの初期化処理はどこに書けば良い?

以下のような形で ViewController を立ち上げる前に実行すればいい様子。


UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffset(horizontal: 0, vertical: 14), for: UIBarMetrics.default)


let vc = OtherViewController()
let viewController = NavigationController()
PlaygroundPage.current.liveView = viewController
PlaygroundPage.current.needsIndefiniteExecution = true

addTaget をしたい

よくある addTarget(self, ...)  のように self を渡すことはできないので、以下のように NSObject を渡して代わりに受け取ってもらいます。

class Responder : NSObject {
    @objc func action() {
        print("Button pressed!")
    }
}
let button = UIButton(frame: CGRect(x: 0, y: 100, width: 100, height: 100))
let responder = Responder()
button.addTarget(responder, action: #selector(responder.action), for: .touchUpInside)
button.backgroundColor = .blue
view.addSubview(button)

alertController を表示したい

ポイントは PlaygroundPage.current.liveView に空の UIViewController を設定した後に viewController で alertController をpresent しているところ。

let alertController = UIAlertController(title: "タイトル", message: "エラー!!!", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default))
let viewController = UIViewController()
PlaygroundPage.current.liveView = viewController
viewController.present(alertController, animated: true, completion: nil)
0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?