0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【SwiftUI】.cornerRadiusが効かない

Posted at

実現しようとしたこと

  • 共通コンポーネントのButtonに角丸を適用する

該当コード

Component.swift
struct ButtonComponents: View {
    @State var buttonText: String
    var body: some View {
        Button(action: {
            print("tap buton")
        }) {
            Text(buttonText)
                .bold()
        }
        .frame(maxWidth: .infinity)
        .frame(maxHeight: 48)
        .cornerRadius(16)
        .accentColor(.white)
        .background(Color.getRawColor(hex: "0017C1"))
    }
}

不具合の原因

  • まず角丸を設定していた順番が良く無い。角丸の後にfillしてしまっているから効いていない。fillしてから角丸を設定する。

  • あとiOS26.1でcornerRadiusがdeprecatedになってるで注意(いつから?2023年には既に非推奨になっていたらしい)。

clipShapeを使いましょうということ

  • 以前からこの方法はありましたが、.cornerRadiusではなくRoundedRectancleを用いて.clipShapeする方法をとりましょう。

直した

Component.swift
struct ButtonComponents: View {
    @State var buttonText: String
    var body: some View {
        Button(action: {
            print("tap buton")
        }) {
            Text(buttonText)
                .bold()
        }
        .frame(maxWidth: .infinity)
        .frame(maxHeight: 48)
        .accentColor(.white)
        .background(Color.getRawColor(hex: "0017C1"))
        .clipShape(RoundedRectangle(cornerRadius: 8))
    }
}
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?