Ios 按钮覆盖显示在按钮文本上,SwiftUI
我的Ios 按钮覆盖显示在按钮文本上,SwiftUI,ios,swift,swiftui,Ios,Swift,Swiftui,我的按钮需要两个覆盖层才能获得所需的样式(笔划、阴影等) 但是,这些覆盖将覆盖按钮文本 视图: 如何调整视图以使蓝色文本显示在背景上方?您可以使用自定义的按钮样式 创建您自己的形状并在其顶部添加.overlay(configuration.label): 您还可以使用configuration.isPressed自定义按钮按下时的标签行为 将其应用于您的按钮: 这使得按钮占据了整个宽度和高度-有没有办法在不设置固定尺寸的情况下进行控制?谢谢。唯一的缺点是它失去了活动状态状态-按下按钮时,按钮不
按钮
需要两个覆盖层才能获得所需的样式(笔划、阴影等)
但是,这些覆盖将覆盖按钮
文本
视图:
如何调整视图以使蓝色文本显示在背景上方?您可以使用自定义的
按钮样式
创建您自己的形状并在其顶部添加.overlay(configuration.label)
:
您还可以使用configuration.isPressed
自定义按钮按下时的标签行为
将其应用于您的按钮
:
这使得按钮占据了整个宽度和高度-有没有办法在不设置固定尺寸的情况下进行控制?谢谢。唯一的缺点是它失去了活动状态
状态-按下按钮时,按钮不透明度降低。不确定是否有任何方法可以用这种方法解决这个问题though@Zorgan您可以使用configuration.isPressed
来检测按钮是否按下,然后相应地采取行动。
struct ProfileTest: View {
var body: some View {
Button(action: {
}){
HStack {
Text("OFFLINE")
.font(.custom("Seravek-Bold", size: 25))
.fontWeight(.bold)
.foregroundColor(Color.blue)
}
}.padding(EdgeInsets(top: 12, leading: 15, bottom: 12, trailing: 15))
.cornerRadius(50)
.overlay(
RoundedRectangle(cornerRadius: 50)
.stroke(Color.black, lineWidth: 1)
)
.overlay(
RoundedRectangle(cornerRadius: 50)
.fill(Color.red)
.shadow(color: Color.black.opacity(1), radius: 1)
)
.opacity(0.7)
.padding(.bottom, 100)
.padding(.bottom, 20)
}
}
struct CustomButtonStyle: ButtonStyle {
func makeBody(configuration: Self.Configuration) -> some View {
configuration.label
.hidden()
.padding(EdgeInsets(top: 12, leading: 15, bottom: 12, trailing: 15))
.cornerRadius(50)
.overlay(
RoundedRectangle(cornerRadius: 50)
.stroke(Color.black, lineWidth: 1)
)
.overlay(
RoundedRectangle(cornerRadius: 50)
.fill(Color.red)
.shadow(color: Color.black.opacity(1), radius: 1)
)
.opacity(0.7)
.overlay(configuration.label)
.padding(.bottom, 100)
.padding(.bottom, 20)
}
}
struct ContentView: View {
var body: some View {
Button(action: {}) {
HStack {
Text("OFFLINE")
.font(.custom("Seravek-Bold", size: 25))
.fontWeight(.bold)
.foregroundColor(Color.blue)
}
}
.buttonStyle(CustomButtonStyle())
}
}