gpt4 book ai didi

ios - SwiftUI 将 navigationBarItems 按钮与大 navigationBarTitle 对齐,如 "Messages"

转载 作者:行者123 更新时间:2023-12-05 05:02:14 26 4
gpt4 key购买 nike

我正在创建一个 SwiftUI 应用程序,我正在使用 NavigationView 和一个大标题栏。但是,我不喜欢 NavigationBarItems 按钮没有与消息应用程序(第一张和第二张图片)中的大标题栏(第三张图片)对齐。我试图重新定位按钮,但它不再是可点击的。有人知道如何解决这个问题吗?谢谢!

Messages app button alignment第二:

Messages app button alignment

第三: Messages app button alignment

最佳答案

解决方案:(在此处找到:https://www.hackingwithswift.com/forums/swiftui/icons-in-navigationview-title-apple-messages-style/592)

import SwiftUI

struct ContentView: View {
@State private var midY: CGFloat = 0.0
@State private var headerText = "Contacts"
var body: some View {
NavigationView {
List {
HStack {
//text
HeaderView(headerText: self.headerText, midY: $midY)
.frame(height: 40, alignment: .leading)
.padding(.top, 5)
.offset(x: -45)

HStack {
//button 1
Button(action: {
self.action1()
}) {
Image(systemName: "ellipsis.circle")
.font(.largeTitle)
}

//button 2
Button(action: {
self.action2()
}) {
Image(systemName: "pencil.circle")
.font(.largeTitle)
}
}.padding(EdgeInsets(top: 5, leading: 0, bottom: 0, trailing: 16))
.foregroundColor(.blue)

} .frame(height: 40, alignment: .leading)
.opacity(self.midY < 70 ? 0.0 : 1.0)
.frame(alignment: .bottom)

ForEach(0..<100){ count in
Text("Row \(count)")
}

}
.navigationBarTitle(self.midY < 70 ? Text(self.headerText) : Text(""), displayMode: .inline)
.navigationBarItems(trailing: self.midY < 70 ? HStack {
//button 1
Button(action: {
self.action1()
}) {
Image(systemName: "ellipsis.circle")
.frame(width: 20, height: 20)
}

//button 2
Button(action: {
self.action2()
}) {
Image(systemName: "pencil.circle")
.frame(width: 20, height: 20)
}
}
:
HStack {
//button 1
Button(action: {
self.action1()
}) {
Image(systemName: "ellipsis.circle")
.frame(width: 0, height: 0)
}

//button 2
Button(action: {
self.action2()
}) {
Image(systemName: "pencil.circle")
.frame(width: 0, height: 0)

}
}
)
}
}

func action1() {
print("do action 1...")
}

func action2() {
print("do action 2...")
}
}

struct HeaderView: View {
let headerText: String
@Binding var midY: CGFloat
var body: some View {
GeometryReader { geometry -> Text in
let frame = geometry.frame(in: CoordinateSpace.global)

withAnimation(.easeIn(duration: 0.25)) {
DispatchQueue.main.async {
self.midY = frame.midY
}
}

return Text(self.headerText)
.bold()
.font(.largeTitle)
}
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

关于ios - SwiftUI 将 navigationBarItems 按钮与大 navigationBarTitle 对齐,如 "Messages",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62277808/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com