SwiftUI:带有图像的按钮在真实 iPhone 上有灰色边框,但在模拟模式下工作正常

发布于 2025-01-09 17:43:49 字数 3143 浏览 1 评论 0 原文

我正在尝试制作简单的应用程序,该应用程序似乎在模拟模式下运行良好,但在我的 iPhone 上测试,按钮看起来与模拟 iPhone 不同,

在此处链接模拟图片,

在此处输入图像描述

这是我在模拟中看到的,但真实的 iPhone 看起来

在此处输入图像描述

,请检查链接的图片,

在子视图结构上制作的按钮图像并作为按钮标签调用,

另外我尝试了无边框按钮样式,背景颜色和前景色与超级视图和子视图相匹配。我什么也没得到,这里是代码,

我不知道如何删除灰色边框,不仅是这些按钮,而且我与图像标签一起使用的所有按钮看起来都一样 - 与灰色矩形边框,

这里是按钮代码

struct MainList: View {
@EnvironmentObject var pageControl: PageControl
var body: some View {
    VStack{
        Button{
            pageControl.pageNum = 1
            pageControl.detailPage = 0
            print("button pressed pageNum: \(pageControl.pageNum), startPage \(pageControl.detailPage)")
        } label: {
            mainListRow(actuator: actuators[0])
                .background(.white)
         }
        .buttonStyle(BorderlessButtonStyle())
        .foregroundColor(.white)

 

,这是用于按钮标签的视图

struct mainListRow: View {
var actuator: Actuator
var body: some View {
    HStack(alignment: .center) {
        Image("home")
        Spacer()
        VStack {
            ZStack{
                RoundedRectangle(cornerRadius: 10.0)
                    .frame(width: 60, height: 30)
                    .foregroundColor(.red)
                Text(String(actuator.speed) + " %")
                    .foregroundColor(.white)
            }
        }
    }
}

}


此处在更改背景颜色时添加另一张图片

在此处输入图像描述< /a>

你可以看到这三个按钮,对于没有背景颜色的顶部按钮,按钮区域填充灰色,如果我添加背景颜色 - 第二个按钮,它只填充内部,所以我将背景颜色更改为黑色的按钮字段,然后边框颜色发生变化。因此,我在按钮字段中添加了白色的背景色,但不起作用 - 第三个按钮:(。测试的手机是 iPhone Xs。这真的很奇怪,因为我提到它在模拟上运行良好。请在真实的 iPhone 上测试它。

这里是代码对于第三张图片,

            Button{
            pageControl.pageNum = 1
            pageControl.detailPage = 0
            print("button pressed pageNum: \(pageControl.pageNum), startPage \(pageControl.detailPage)")
        } label: {
            mainListRow(actuator: actuators[0])
        }
        .buttonStyle(BorderlessButtonStyle())
        Divider()
        Button{
            pageControl.pageNum = 1
            pageControl.detailPage = 1
            print("Rotate button pressed pageNum: \(pageControl.pageNum), startPage \(pageControl.detailPage)")
        } label: {
            mainListRow(actuator: actuators[1])
                .background(.white)
        }
        .background(.black)
        Divider()
        Button{
            pageControl.pageNum = 1
            pageControl.detailPage = 2
            print("Rotate button pressed pageNum: \(pageControl.pageNum), startPage \(pageControl.detailPage)")
        } label: {
            mainListRow(actuator: actuators[2])
                .buttonStyle(BorderlessButtonStyle())
        }
        .background(.white)

按钮页面添加到此处的主页面

struct testPage: View {
var body: some View {
    MainList()
        .frame(width: 330, height: 150)
        .padding(.top, 20)
}

}

I'm trying to make simple app, the app seems working well on simulation mode, but test on my iPhone the button looks different from simulation iPhone,

link the simulation picture here,

enter image description here

this is the what I see on the simulation but real iPhone looks

enter image description here

please, check the pictures linked,

The button image made on sub View struct and called as a Button label,

plus I tried borderlessbuttonstyle, back and foreground color match both super, and child view. I got nothing, here the code,

I don't know how to delete the gray border not only these buttons but also all buttons that I used with image labeling looks the same way - bordered with gray rectangle,

here the button code

struct MainList: View {
@EnvironmentObject var pageControl: PageControl
var body: some View {
    VStack{
        Button{
            pageControl.pageNum = 1
            pageControl.detailPage = 0
            print("button pressed pageNum: \(pageControl.pageNum), startPage \(pageControl.detailPage)")
        } label: {
            mainListRow(actuator: actuators[0])
                .background(.white)
         }
        .buttonStyle(BorderlessButtonStyle())
        .foregroundColor(.white)

 

and this is the View that used to button label

struct mainListRow: View {
var actuator: Actuator
var body: some View {
    HStack(alignment: .center) {
        Image("home")
        Spacer()
        VStack {
            ZStack{
                RoundedRectangle(cornerRadius: 10.0)
                    .frame(width: 60, height: 30)
                    .foregroundColor(.red)
                Text(String(actuator.speed) + " %")
                    .foregroundColor(.white)
            }
        }
    }
}

}


here add the another picture when I change the background color

enter image description here

as you can see that those three buttons, for the top button without background color, the button area is filled gray color, if I add background color - second button, it only filled inside, so I changed the background color to button field with black then the border color changed. thus I add the background color with white at button field not working - third button :(. the tested phone is iPhone Xs. this is really weird as I mentioned it work fine on simulation. please. test it on real iPhone.

here the code for third picture,

            Button{
            pageControl.pageNum = 1
            pageControl.detailPage = 0
            print("button pressed pageNum: \(pageControl.pageNum), startPage \(pageControl.detailPage)")
        } label: {
            mainListRow(actuator: actuators[0])
        }
        .buttonStyle(BorderlessButtonStyle())
        Divider()
        Button{
            pageControl.pageNum = 1
            pageControl.detailPage = 1
            print("Rotate button pressed pageNum: \(pageControl.pageNum), startPage \(pageControl.detailPage)")
        } label: {
            mainListRow(actuator: actuators[1])
                .background(.white)
        }
        .background(.black)
        Divider()
        Button{
            pageControl.pageNum = 1
            pageControl.detailPage = 2
            print("Rotate button pressed pageNum: \(pageControl.pageNum), startPage \(pageControl.detailPage)")
        } label: {
            mainListRow(actuator: actuators[2])
                .buttonStyle(BorderlessButtonStyle())
        }
        .background(.white)

and the the button page added to main here

struct testPage: View {
var body: some View {
    MainList()
        .frame(width: 330, height: 150)
        .padding(.top, 20)
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执着的年纪 2025-01-16 17:43:49

按钮上的灰色/覆盖层来自按钮“色调”。

要删除它,请添加 .plain 修饰符:

Button {
}
.buttonStyle(.plain)

截至今天Xcode 中存在错误,这就是为什么您无法在模拟器或预览中正确看到色调

更多解决方法示例:

https://hackingwithswift.com/quick-start/swiftui/how-to-disable-the-overlay-color-for-images-inside-button-and-navigationlink

The gray/overlay on the button is from the button "tint".

To remove it, add the .plain modifier:

Button {
}
.buttonStyle(.plain)

As of today there is a bug in Xcode and that's why you can't see the tint properly in the simulator or the preview.

More examples of workarounds here:

https://hackingwithswift.com/quick-start/swiftui/how-to-disable-the-overlay-color-for-images-inside-button-and-navigationlink

荒路情人 2025-01-16 17:43:49

MainList 中删除:

            .background(.white)

in MainList get rid of:

            .background(.white)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文