如何解决SwifUI LandScape NavigationTopBar不会隐藏
在所有视图之间我只需要一个风景视图,因此我在AppDelegate中添加了以下代码
func application(_ application: UIApplication,supportedInterfaceOrientationsFor window:
UIWindow?) -> UIInterfaceOrientationMask {
return AppDelegate.orientationLock
}
并且我已经创建了一个像波纹管这样的自定义修改器,以强制特定视图处于横向模式
struct LandScapeOrientation: ViewModifier {
func body(content: Content) -> some View {
content
.onAppear {
AppDelegate.orientationLock = UIInterfaceOrientationMask.landscapeLeft
UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue,forKey: "orientation")
UINavigationController.attemptRotationToDeviceOrientation()
}
.onDisappear {
DispatchQueue.main.async {
AppDelegate.orientationLock = UIInterfaceOrientationMask.portrait
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue,forKey: "orientation")
UINavigationController.attemptRotationToDeviceOrientation()
}
}
}}
extension View {
func landScape() -> some View{
self.modifier(LandScapeOrientation())
}
}
我创建的视图就像下面这样:
struct GameplayScene: View {
//MARK: - Var
@State var round = NeverMindRound.first
@Binding var mode: NevermindMode
//MARK: - Views
private var background: some View {
colors.customYellowOk.value
.edgesIgnoringSafeArea(.all)
}
//MARK: - MainBody
var body: some View {
return ZStack{
background
RoundIntro(round: $round)
.landScape()
.navigationBarHidden(true)
.navigationBarBackButtonHidden(true)
.statusBar(hidden: true)
}.navigationBarHidden(true)
.navigationBarBackButtonHidden(true)
.statusBar(hidden: true)
}}
所以问题在于,以横向模式显示此视图后,导航栏将显示,即使我指定应将其隐藏,也无法将其隐藏。
这是GamePlay展示的rounIntro视图
struct RoundIntro: View {
//MARK: - Vars
@Binding var round: NeverMindRound
//MARK: - View
private var roundTitle: some View {
var shadow = colors.round1TitleShadow.value
var roundTitle = "Round 1"
switch round {
case .first:
roundTitle = "Round 1"
shadow = colors.round1TitleShadow.value
case .second:
roundTitle = "Round 2"
shadow = colors.round2TitleShadow.value
case .third:
roundTitle = "Round 3"
shadow = colors.round3TitleShadow.value
}
return Text(roundTitle)
.font(rubik.black.with(Size: 118))
.foregroundColor(.white)
.shadow(color: shadow,radius: 6,x: -7,y: 7)
}
//MARK: - MainBody
var body: some View {
roundTitle
.landScape()
}
}
另一件事是,在按下导航栏的后退按钮后,它不会强制视图返回纵向模式。 这是情况的照片:
解决方法
您已将导航栏设置为
struct RoundIntro: View {
但是您应该为各自的UIViewController的导航栏设置hidden = true属性
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。