SwiftUI LandScape 导航顶栏不会隐藏
Posted
技术标签:
【中文标题】SwiftUI LandScape 导航顶栏不会隐藏【英文标题】:SwifUI LandScape NavigationTopBar won't hide 【发布时间】:2020-09-15 08:14:15 【问题描述】:我想要所有视图之间只有一个横向视图,所以我在 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)
所以问题是在横向模式下显示此视图后,navigationBar 将显示,即使我指定它应该隐藏,我也无法隐藏它..
这是 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()
另一件事是在按下导航栏后退按钮后,它不会强制视图返回纵向模式。 以下是现场照片:
【问题讨论】:
【参考方案1】:你已经设置了导航栏
struct RoundIntro: View
但是你应该将 hidden = true 属性设置为相应的 UIViewController 的导航栏
【讨论】:
只有在 iphone 11 pro max 和 iphone 8 plus 上才这样......以上是关于SwiftUI LandScape 导航顶栏不会隐藏的主要内容,如果未能解决你的问题,请参考以下文章