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

【SwiftUI】角丸ボタンを作る。

Last updated at Posted at 2023-09-16

備忘録

背景

下記のAPIで角丸ボタン作れるみたいだが、Deprecatedになっている
https://developer.apple.com/documentation/familycontrols/familyactivityiconview/cornerradius(_:antialiased:)

そのため、角丸ボタンを他の方法で作ったけど少し時間が掛かったのでQiitaに残しておく

ソースコード

struct RoundedRectangleButton: View {
    // 各パラメータ
    var text: String
    var textColor: Color
    var backgroundColor: Color
    var fontWeight: Font.Weight
    var cornerRadius: CGFloat
    var width: CGFloat
    var height: CGFloat
    var action: () -> Void
    
    var body: some View {
        Button(action: { action() }, label: {
            ZStack(content: {
                RoundedRectangle(cornerRadius: cornerRadius)
                    .fill(backgroundColor)
                    .frame(width: width, height: height)
                
                Text(text)
                    .foregroundColor(textColor)
                    .fontWeight(fontWeight)
            })
        })
    }
}

struct HogeView: View {
    var body: some View {
    // do something...

    RoundedRectangleButton(text: "リセット",
        foregroundColor: .white,
        backgroundColor: .red,
        fontWeight: .semibold,
        cornerRadius: 10,
        width: view.size.width / 2,
        height: 48) {
            // ボタンアクション
        }
    }
}

下記のようなボタンが作れるようになる
IMG_0832.jpg

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