2
2

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 3 years have passed since last update.

SwiftUIのAlignment構造体について

Last updated at Posted at 2020-05-05

##Alignment構造体の配置
SwiftUIではViewのどこにTextなどのオブジェクトを配置するか決めることができます。スクリーンショット 2020-05-05 19.46.52.png

Viewに対してモディファイアである.frame(width: , height: , alignment: )を指定することで幅と高さ、オブジェクトの位置を決めることができます。

課題点としては画面外に座標が広がっているのを直さないといけないところですね。
もっとSwiftUIの良さを生かせば短く可読性の高いコードが書けそうなのです...

import SwiftUI

var widthSize = UIScreen.main.bounds.size.width
var heightSize = UIScreen.main.bounds.size.height
struct ColorPicker: View {
    var body: some View {
        
        VStack {
            HStack {
                Text("center")
                    .foregroundColor(Color.white)
                    .frame(width: UIScreen.main.bounds.size.width/3, height: UIScreen.main.bounds.size.height/3, alignment: .center)
                    .background(Color.black)
                Spacer()
                Text("leading")
                    .foregroundColor(Color.white)
                    .frame(width: UIScreen.main.bounds.size.width/3, height: UIScreen.main.bounds.size.height/3, alignment: .leading)
                    .background(Color.black)
                Spacer()
                Text("trailing")
                    .foregroundColor(Color.white)
                    .frame(width: UIScreen.main.bounds.size.width/3, height: UIScreen.main.bounds.size.height/3,alignment: .trailing)
                    .background(Color.black)
            }
            
            Spacer()
             HStack {
                 Text("top")
                    .foregroundColor(Color.white)
                    .frame(width: UIScreen.main.bounds.size.width/3, height: UIScreen.main.bounds.size.height/3, alignment: .top)
                    .background(Color.black)
                 Spacer()
                 Text("bottom")
                    .foregroundColor(Color.white)
                    .frame(width: UIScreen.main.bounds.size.width/3, height: UIScreen.main.bounds.size.height/3, alignment: .bottom)
                    .background(Color.black)
                 Spacer()
                 Text("topLeading")
                    .foregroundColor(Color.white)
                    .frame(width: UIScreen.main.bounds.size.width/3, height: UIScreen.main.bounds.size.height/3, alignment: .topLeading)
                    .background(Color.black)
                 
            }
            Spacer()
            HStack {
                Text("topTrailing")
                    .foregroundColor(Color.white)
                    .frame(width: UIScreen.main.bounds.size.width/3, height: UIScreen.main.bounds.size.height/3, alignment: .topLeading)
                    .background(Color.black)
                Spacer()
                Text("bottomLeading")
                    .foregroundColor(Color.white)
                    .frame(width: UIScreen.main.bounds.size.width/3, height: UIScreen.main.bounds.size.height/3, alignment: .bottomLeading)
                .background(Color.black)
                Spacer()
                Text("a")
                    .foregroundColor(Color.white)
                    .frame(width: UIScreen.main.bounds.size.width/3, height: UIScreen.main.bounds.size.height/3, alignment: .bottomTrailing)
                    .background(Color.black)
                    

            }
        }
    }
}
struct ColorPicker_Previews: PreviewProvider {
    static var previews: some View {
        ColorPicker()
    }
}


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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?