问题描述
我正在尝试设计带有呼叫按钮的导航栏。但是,标准导航栏从大到内联,滚动时,这使得呼叫按钮不再适合导航栏?有没有办法阻止导航栏在滚动时切换?或者是否有一种特定的方法可以在滚动时将按钮保留在导航栏上?任何帮助将不胜感激!谢谢!
这就是我目前正在使用的!
.navigationBarTitle("Elevate",displayMode: .large)
.navigationBarItems(trailing:
HStack
{
Button(action: {
if let url = NSURL(string: "tel://\(1234567890)"),UIApplication.shared.canopenURL(url as URL) {
UIApplication.shared.openURL(url as URL)
}
print("Edit button was tapped")})
{
Image(systemName: "phone.fill")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 30)
.padding(.top)
.accentColor(.black)
}
Button(action: {
print("Message button was tapped")})
{
Image(systemName: "message.fill")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 30)
.padding(.top)
.accentColor(.black)
}
Button(action: {
print("Settings button was tapped")})
{
Image(systemName: "gearshape.fill")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 30)
.padding(.top)
.accentColor(.black)
}
})
解决方法
如果您使用的是 SwiftUI,则需要创建一个具有 .inline 显示模式的 NavigationView,这样您就不会使用 .large 并滚动显示大标题。
struct ContentView: View {
var body: some View {
NavigationView {
Text("This is a View!")
.navigationBarTitle("Elevate",displayMode: .inline) //set inline display
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: {
if let url = NSURL(string: "tel://\(1234567890)"),UIApplication.shared.canOpenURL(url as URL) {
UIApplication.shared.openURL(url as URL)
}
print("Edit button was tapped")})
{
Image(systemName: "phone.fill")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 30)
.padding(.top)
.accentColor(.black)
}
}
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: {
print("Message button was tapped")})
{
Image(systemName: "message.fill")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 30)
.padding(.top)
.accentColor(.black)
}
}
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: {
print("Settings button was tapped")})
{
Image(systemName: "gearshape.fill")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 30)
.padding(.top)
.accentColor(.black)
}
}
}
}
}
}
如下所示:
使用 .large 而不是 .inline:
编辑:显示 SwiftUI 而不是 UIKit - 感谢 @jnpdx 指出 OP 的原始请求。