LENLY-LAB

>>>>

ЧАСТЫЕ ВОПРОСЫ

SWIFTUI

SwiftUI: как запретить автоматическую ориентацию экрана для отдельного View

Стандартных методов для этого нет, поэтому добавляем класс в любое место вне видов.

class AppDelegate: NSObject, UIApplicationDelegate {static var orientationLock =

UIInterfaceOrientationMask.all //By default you want all your views to rotate freely

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?)->

UIInterfaceOrientationMask{return AppDelegate.orientationLock}}


Добавляем адаптор в основной вид приложения

@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

И в любой вид где нужно ограничить ориентацию добавляем событие .onApear


Получается примерно вот так:

import SwiftUI

//класс для фиксирования ориентации

class AppDelegate: NSObject, UIApplicationDelegate {static var orientationLock

= UIInterfaceOrientationMask.all //By default you want all your views to rotate freely

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?)->

UIInterfaceOrientationMask{return AppDelegate.orientationLock}}

@main

struct myCreditApp: App {

@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate//добавляем адаптор для делегата


var body: some Scene {

WindowGroup {

ZStack{

ContentView1()

.onAppear {UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation") //

Forcing the rotation to portrait

AppDelegate.orientationLock = .portrait} // And making sure it stays that way

ContentView2().onAppear {AppDelegate.orientationLock = .all}

}

}}}


Это установка портретного вида для ContentView1().

Но так же можно настроить и ландшафтный вид.

Связаться с нами

Web-studio LENLY-LAB

Email: task@lenly-lab.ru

г. Москва