Swift Button内のビューを右に寄せるサンプル

環境
Swift version 5.2.3 (swift-5.2.3-RELEASE)

構文
Button(“xxx"){
//処理コード
}
.frame(width: /*横幅*/, alignment: .trailing)
frameモディファイアの引数「width」に横幅、引数「alignment」に「.trailing」を指定します。
frameモディファイアを使えば、Button内のビューを右に寄せます。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import SwiftUI
struct SampleView: View {
var body: some View {
VStack{
Button("Show"){
print("Tap")
}
.frame(width: 200, alignment: .trailing)
.padding()
.background(Color.green)
.foregroundColor(Color.white)
}
}
}
import SwiftUI struct SampleView: View { var body: some View { VStack{ Button("Show"){ print("Tap") } .frame(width: 200, alignment: .trailing) .padding() .background(Color.green) .foregroundColor(Color.white) } } }
import SwiftUI

struct SampleView: View {
    
    var body: some View {
        VStack{
            Button("Show"){
                print("Tap")
            }
            .frame(width: 200, alignment: .trailing)
            .padding()
            .background(Color.green)
            .foregroundColor(Color.white)
        }
    }
}

 

IT

Posted by arkgame