LoginSignup
8
3

More than 5 years have passed since last update.

Storyboardを使わずにコードでレイアウトするときに使えるSourceEditorExtension

Last updated at Posted at 2018-04-17

SwiftでStoryboardを使わずにコードだけで画面のレイアウトを書く際に、毎回おまじないのように書くコード(translatesAutoresizingMaskIntoConstraintsやNSLayoutConstraintなど)が多いので、コマンド一発である程度補完できるようになる、ちょっとだけ便利なSourceEditorExtensionを作ってみました。

output.gif

選択範囲にある変数に対して、translatesAutoresizingMaskIntoConstraints と、 topAnchor, leftAnchor, rightAnchor, bottomAnchor の制約を一括で書くことができます。ちなみに複数選択にも対応しています。


sampleView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
    sampleView.topAnchor.constraint(equalTo: <#targetView#>.topAnchor),
    sampleView.leftAnchor.constraint(equalTo: <#targetView#>.leftAnchor),
    sampleView.rightAnchor.constraint(equalTo: <#targetView#>.rightAnchor),
    sampleView.bottomAnchor.constraint(equalTo: <#targetView#>.bottomAnchor)
    ])

リポジトリからgit cloneしてarchive buildした中身を開くとxcode上で使えるようになるので、お好きなキーバインドを割り当てて使ってみてください。中身のコードは結構簡単なのでカスタムしてもいいと思います。よかったらgitのスターください。笑

8
3
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
8
3