LoginSignup
0
0

More than 3 years have passed since last update.

Viewの制約が(結果として同じもの)を設定するときの状況別のパターン

Last updated at Posted at 2019-12-15

動機

Youtubeチャンネルの素材用にTinderの模擬アプリを作成する際、
Viewの制約を設定するとき、結果として同じものがいくつかあり
どのパターンで制約を設定するか迷うことが個人的によくあります
->後になって設計や機能が詰まってきた時にあっちのパターンで制約かけておけばよかった。。。ということもしばしば。。

今後それをなるべく無くしたいので定期的にメモがわりに残します

状況と対策

superViewに対してViewにmarginをかける&中心は揃える

この場合パターンとして

  • 中心座標をsuperViewに揃える & Viewのwidth, heightをsuperViewwidht, height-marginにする
  • superViewに対してViewの縁(leading~bottom)をmargin分縮めたものにする

girlsViewArray[$0].leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 10.0).isActive = true
girlsViewArray[$0].trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -10.0).isActive = true
girlsViewArray[$0].topAnchor.constraint(equalTo: self.view.topAnchor, constant: 74.0).isActive = true
            //84.0 = 44.0(navHeight) + 20.0(statusBarHeight) + 10(margin)
girlsViewArray[$0].bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -20.0).isActive = true

が挙げられると思います

個人的には
- 中心座標superViewに揃える & superViewwidht, height-marginにする
->Viewの座標が動く時(ex.ドラッグや大きさの変化)
- superViewに対してmargin分縮めたものにする
->Viewの座標が動かないと決まっている時

またわかったら追記します

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