ios - 尝试在右上角格式化图像 - SwiftUI
问题描述
我正在尝试使用右上角的设置按钮对 iOS 应用程序进行编程。我尝试使用 aSpacer
让 SF Symbol 图像保持在右上角,但是当我使用单个垫片时,它会将图像推到屏幕边界之外。我是否可以添加约束以将图像保持在右上角,而不会超出屏幕边界?如果可能的话,将图像保留在所有 iOS 设备的右上角?我对 SwiftUI 非常陌生,因此感谢您提供任何见解和建议!
这是我正在使用的代码:
import SwiftUI
struct ContentView: View {
var body: some View {
// view code
ZStack {
Image("Background").resizable()
.aspectRatio(contentMode: .fill).ignoresSafeArea()
VStack{
HStack {
Button(action: {}, label: {
Image(systemName: "gear").resizable().aspectRatio(contentMode: .fit)
.frame(width: 50, height: 50) })
}
.padding()
Spacer()
HStack {
Button(action: {}, label: {
Image(systemName: "plus")
.resizable().aspectRatio(contentMode: .fit).frame(width: 150, height: 150, alignment: .center).padding().border(Color.white, width: 8).cornerRadius(10.0).foregroundColor(.white)
})
}
Spacer()
}
}
}
带垫片 | 无垫片 |
---|---|
解决方案
编辑:因为您的图像在 a 中ZStack
,它会扩展并溢出屏幕。您应该改用background
修饰符。
struct ContentView: View {
var body: some View {
VStack{
HStack {
Spacer()
Button(action: {}, label: {
Image(systemName: "gear").resizable().aspectRatio(contentMode: .fit)
.frame(width: 50, height: 50) })
}
.padding()
Spacer()
HStack {
Button(action: {}, label: {
Image(systemName: "plus")
.resizable().aspectRatio(contentMode: .fit).frame(width: 150, height: 150, alignment: .center).padding().border(Color.white, width: 8).cornerRadius(10.0).foregroundColor(.white)
})
}
Spacer()
}
/// here!
.background(
Image("Background")
.resizable()
.aspectRatio(contentMode: .fill)
.ignoresSafeArea()
)
}
}
结果:
如果您使用 aZStack
对背景进行分层,则其中的视图可能会超出边缘。我试图在下面重现您想要的内容,但如果您发布更多代码会有所帮助。
struct ContentView: View {
var body: some View {
ZStack {
LinearGradient(gradient: Gradient(colors: [.blue, .green]), startPoint: .top, endPoint: .bottom)
.ignoresSafeArea()
Image(systemName: "plus")
.resizable()
.foregroundColor(.white)
.frame(width: 200, height: 200)
VStack{
HStack {
Spacer()
Button(action: { }) {
Image(systemName: "gear")
.resizable()
.foregroundColor(.white)
.frame(width: 50, height: 50)
}
}
Spacer()
}
}
}
}
结果:
推荐阅读
- if-statement - 即使我输入了 if 的正确答案,它也会显示 else 的答案
- firebase - 获取好友列表,然后显示好友列表
- android - 将数据从我的片段发送到 Viewmodel 时片段未附加到活动
- python - 遍历范围并返回不在任何范围内的范围?
- ios - 我用 UIBezierPath 和 CAShapeLayer 制作的箭头如何指向 UILabel 的确切中心?
- java - 为什么 Querydsl 在阴影 jar 中不翻译 java.time.* 列?
- java - CheckMarx 扫描中的未关闭对象警报
- javascript - 我想根据我的父 ID 类别获取所有子类别。使用 Django/Vuejs。更多信息如下
- docker - 如何在 GitHub Actions 中失败时重新运行 docker 容器,但工作流仍然通过?
- reactjs - antd form.item 需要条件