Jak wymusić orientację kontrolera widoku w systemie iOS 8?
Przed iOS 8, użyliśmy poniższego kodu w połączeniu z supportedInterfaceOrientations i powinno się delegować metody , aby wymusić orientację aplikacji na dowolną konkretną orientację. Użyłem poniższego fragmentu kodu, aby programowo obracać aplikację do pożądanej orientacji. Po pierwsze, zmieniam orientację paska stanu. A następnie po prostu prezentowanie i natychmiastowe oddalanie widoku modalnego obraca widok do pożądanej orientacji.
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES];
UIViewController *c = [[UIViewController alloc]init];
[self presentViewController:vc animated:NO completion:nil];
[self dismissViewControllerAnimated:NO completion:nil];
Ale to zawodzi w iOS 8. Widziałem również kilka odpowiedzi w Stack overflow, gdzie ludzie sugerowali, że powinniśmy zawsze unikać tego podejścia od iOS 8.
Mówiąc dokładniej, moja aplikacja jest uniwersalnym typem aplikacji. W sumie są trzy Kontrolery.
Kontroler pierwszego widoku - powinien obsługiwać wszystkie orientacje na iPadzie i tylko portret (przycisk home w dół) w iPhonie.
-
Drugi kontroler widoku - powinien obsługiwać tylko krajobraz prawo w każdych warunkach
-
Trzeci kontroler widoku - powinien obsługiwać tylko prawy krajobraz w każdych warunkach
Używamy kontrolera nawigacji do nawigacji strony. Z pierwszego kontrolera widoku, po kliknięciu przycisku, naciskamy drugi na stos. Tak więc, gdy pojawi się drugi kontroler widoku, niezależnie od orientacji urządzenia, aplikacja powinna zablokować się tylko w poziomie.
Poniżej moje metody shouldAutorotate
i supportedInterfaceOrientations
w drugim i trzeci kontroler widoku.
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscapeRight;
}
-(BOOL)shouldAutorotate {
return NO;
}
Czy jest jakieÅ› rozwiÄ…zanie dla tego lub innego lepszego sposobu blokowania kontrolera widoku w konkretnej orientacji dla iOS 8. ProszÄ™ o pomoc!!
24 answers
Dla iOS 7-10:
Objective-C:
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeLeft) forKey:@"orientation"];
[UINavigationController attemptRotationToDeviceOrientation];
Swift 3:
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
UINavigationController.attemptRotationToDeviceOrientation()
Wystarczy wywołać go w - viewDidAppear:
prezentowanego kontrolera widoku.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-14 03:43:51
Obrót orientacji jest nieco bardziej skomplikowany, jeśli jesteś wewnątrz UINavigationController
lub UITabBarController
. Problem polega na tym, że jeśli kontroler widoku jest osadzony w jednym z tych kontrolerów, kontroler nawigacji lub paska kart ma pierwszeństwo i podejmuje decyzje dotyczące autorotacji i obsługiwanych orientacji.
Używam następujących 2 rozszerzeń na UINavigationController i UITabBarController tak, że kontrolery widoku, które są osadzone w jednym z tych kontrolerów, mogą decyzje.
Daj kontrolerom widoku moc!
Swift 2.3
extension UINavigationController {
public override func supportedInterfaceOrientations() -> Int {
return visibleViewController.supportedInterfaceOrientations()
}
public override func shouldAutorotate() -> Bool {
return visibleViewController.shouldAutorotate()
}
}
extension UITabBarController {
public override func supportedInterfaceOrientations() -> Int {
if let selected = selectedViewController {
return selected.supportedInterfaceOrientations()
}
return super.supportedInterfaceOrientations()
}
public override func shouldAutorotate() -> Bool {
if let selected = selectedViewController {
return selected.shouldAutorotate()
}
return super.shouldAutorotate()
}
}
Swift 3
extension UINavigationController {
open override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return visibleViewController?.supportedInterfaceOrientations ?? super.supportedInterfaceOrientations
}
open override var shouldAutorotate: Bool {
return visibleViewController?.shouldAutorotate ?? super.shouldAutorotate
}
}
extension UITabBarController {
open override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
if let selected = selectedViewController {
return selected.supportedInterfaceOrientations
}
return super.supportedInterfaceOrientations
}
open override var shouldAutorotate: Bool {
if let selected = selectedViewController {
return selected.shouldAutorotate
}
return super.shouldAutorotate
}
}
Teraz możesz nadpisać metodę supportedInterfaceOrientations
lub nadpisać shouldAutoRotate
w kontrolerze widoku, który chcesz zablokować, w przeciwnym razie możesz pominąć nadpisania w innych kontrolerach widoku, które chcesz dziedziczyć domyślne zachowanie orientacji określone w plist aplikacji
Wyłącz Rotację
class ViewController: UIViewController {
override func shouldAutorotate() -> Bool {
return false
}
}
Blokada do konkretnego Orientacja
class ViewController: UIViewController {
override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.Landscape.rawValue)
}
}
Teoretycznie powinno to działać dla wszystkich złożonych hierarchii kontrolerów widoku, ale zauważyłem problem z UITabBarController. Z jakiegoś powodu chce użyć domyślnej wartości orientacyjnej. Zapoznaj się z poniższym wpisem na blogu, jeśli chcesz dowiedzieć się, jak obejść niektóre z problemów:
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-21 07:25:59
To mi się udało:
Wywołaj to w twoim poglądudidappear: method.
- (void) viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[UIViewController attemptRotationToDeviceOrientation];
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-18 16:52:40
Odkryłem, że jeśli jest to prezentowany kontroler widoku, możesz nadpisać preferredInterfaceOrientationForPresentation
Swift:
override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.Landscape.rawValue)
}
override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
return UIInterfaceOrientation.LandscapeLeft
}
override func shouldAutorotate() -> Bool {
return false
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-18 07:38:40
Ten sposób działa dla mnie w Swift 2 iOS 8.x:
PS (ta metoda nie wymaga nadpisywania funkcji orientacji, takich jak shouldautorotate na każdym kontrolerze viewController, tylko jednej metody na AppDelegate)
Zaznacz "wymaga pełnego ekranu" w projekcie ogólne informacje.
Więc, na AppDelegate.swift tworzy zmienną:
var enableAllOrientation = false
Więc, umieścić również ten func:
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
if (enableAllOrientation == true){
return UIInterfaceOrientationMask.All
}
return UIInterfaceOrientationMask.Portrait
}
Więc w każdej klasie w Twoim projekcie możesz ustawić ten var w viewWillAppear:
override func viewWillAppear(animated: Bool)
{
super.viewWillAppear(animated)
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.enableAllOrientation = true
}
Jeśli chcesz dokonać wyboru w oparciu o typ urządzenia, możesz to zrobić:
override func viewWillAppear(animated: Bool)
{
super.viewWillAppear(animated)
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
switch UIDevice.currentDevice().userInterfaceIdiom {
case .Phone:
// It's an iPhone
print(" - Only portrait mode to iPhone")
appDelegate.enableAllOrientation = false
case .Pad:
// It's an iPad
print(" - All orientation mode enabled on iPad")
appDelegate.enableAllOrientation = true
case .Unspecified:
// Uh, oh! What could it be?
appDelegate.enableAllOrientation = false
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-07 09:17:14
To powinno działać z iOS 6 w górę, ale Przetestowałem go tylko na iOS 8. Podklasa UINavigationController
i nadpisuje następujące metody:
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeRight;
}
- (BOOL)shouldAutorotate {
return NO;
}
Lub zapytaj kontroler widocznego widoku
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return self.visibleViewController.preferredInterfaceOrientationForPresentation;
}
- (BOOL)shouldAutorotate {
return self.visibleViewController.shouldAutorotate;
}
I wdrożyć tam metody.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-31 15:45:01
Jest to odpowiedź na komentarze w Sid Shah ' s answer , dotyczące wyłączania animacji za pomocą:
[UIView setAnimationsEnabled:enabled];
Kod:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:NO];
[UIView setAnimationsEnabled:NO];
// Stackoverflow #26357162 to force orientation
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:NO];
[UIView setAnimationsEnabled:YES];
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:18:22
Po pierwsze-jest to zły pomysł, w ogóle coś nie tak z architekturą aplikacji, ale, sh..t happens
, jeśli tak, możesz spróbować zrobić coś takiego jak poniżej:
final class OrientationController {
static private (set) var allowedOrientation:UIInterfaceOrientationMask = [.all]
// MARK: - Public
class func lockOrientation(_ orientationIdiom: UIInterfaceOrientationMask) {
OrientationController.allowedOrientation = [orientationIdiom]
}
class func forceLockOrientation(_ orientation: UIInterfaceOrientation) {
var mask:UIInterfaceOrientationMask = []
switch orientation {
case .unknown:
mask = [.all]
case .portrait:
mask = [.portrait]
case .portraitUpsideDown:
mask = [.portraitUpsideDown]
case .landscapeLeft:
mask = [.landscapeLeft]
case .landscapeRight:
mask = [.landscapeRight]
}
OrientationController.lockOrientation(mask)
UIDevice.current.setValue(orientation.rawValue, forKey: "orientation")
}
}
Niż, w AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// do stuff
OrientationController.lockOrientation(.portrait)
return true
}
// MARK: - Orientation
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return OrientationController.allowedOrientation
}
I kiedy chcesz zmienić orientację rób jak:
OrientationController.forceLockOrientation(.landscapeRight)
Uwaga: czasami urządzenie może nie aktualizować się z takiego połączenia, więc może być konieczne wykonanie następujących czynności
OrientationController.forceLockOrientation(.portrait)
OrientationController.forceLockOrientation(.landscapeRight)
To wszystko
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-06 10:16:24
Na Xcode 8
Metody są konwertowane do właściwości, więc z Swift
działa:
override public var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.portrait
}
override public var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return UIInterfaceOrientation.portrait
}
override public var shouldAutorotate: Bool {
return true
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-09-21 12:49:30
Jeśli używasz navigationViewController, powinieneś utworzyć własną superklasę i nadpisać:
- (BOOL)shouldAutorotate {
id currentViewController = self.topViewController;
if ([currentViewController isKindOfClass:[SecondViewController class]])
return NO;
return YES;
}
To wyłączy obrót w SecondViewController ale jeśli naciśniesz swój SecondViewController Gdy Twoje urządzenie jest w orientacji pionowej, twój SecondViewController pojawi się w trybie pionowym.
Załóżmy, że używasz storyboard. Musisz utworzyć manual segue (How to ) i w swoim "onClick" "metoda": {]}
- (IBAction)onPlayButtonClicked:(UIBarButtonItem *)sender {
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
[self performSegueWithIdentifier:@"PushPlayerViewController" sender:self];
}
Wymusi to orientację poziomą przed wyłączeniem funkcji autorotate superclass.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:18:22
Próbowałem wielu rozwiązań, ale ten, który zadziałał, jest następujący:
Nie ma potrzeby edytowaniainfo.plist
w ios 8 i 9.
- (BOOL) shouldAutorotate {
return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return (UIInterfaceOrientationPortrait | UIInterfaceOrientationPortraitUpsideDown);
}
Możliwe orientacje z Dokumentacji Apple :
UIInterfaceOrientationUnknown
Nie można określić orientacji urządzenia.UIInterfaceOrientationPortrait
[[2]}urzÄ…dzenie jest w trybie portretowym, z urzÄ…dzeniem trzymanym pionowo i przycisk home na dole.UIInterfaceOrientationPortraitUpsidedown
[[2]}urządzenie jest w trybie portretowym, ale do góry nogami, z urządzeniem trzymanym w pozycji pionowej i przycisk home u góry.UIInterfaceOrientationLandscapeLeft
[[2]}urzÄ…dzenie jest w trybie poziomym, z urzÄ…dzeniem trzymanym w pozycji pionowej i przycisk home po lewej stronie.UIInterfaceOrientationLandscapeRight
UrzÄ…dzenie jest w tryb krajobrazowy, z urzÄ…dzeniem trzymanym w pozycji pionowej i przycisk home po prawej stronie.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-22 14:18:58
Kombinacja odpowiedzi Sids i Koreys zadziałała dla mnie.
Rozszerzenie kontrolera Nawigacji:
extension UINavigationController {
public override func shouldAutorotate() -> Bool {
return visibleViewController.shouldAutorotate()
}
}
Następnie wyłączanie rotacji w widoku pojedynczym
class ViewController: UIViewController {
override func shouldAutorotate() -> Bool {
return false
}
}
I obracajÄ…c siÄ™ do odpowiedniej orientacji przed segue
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "SomeSegue")
{
let value = UIInterfaceOrientation.Portrait.rawValue;
UIDevice.currentDevice().setValue(value, forKey: "orientation")
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-02 17:59:49
Górne rozwiązanie powyżej:
let value = UIInterfaceOrientation.LandscapeLeft.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
Nie działa dla mnie, gdy wywołałem go w viewDidAppear prezentowanego kontrolera widoku. Jednak zadziałało, gdy zadzwoniłem do niego w preparForSegue w prezentującym kontrolerze widoku.
(Sorry, za mało punktów reputacji, aby skomentować to rozwiązanie, więc musiałem dodać to w ten sposób)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-05-07 21:59:20
Moje wymagania to
- blokowanie wszystkich widoków w trybie portretowym
- użyj
AVPlayerViewController
do odtwarzania wideo
Podczas odtwarzania wideo, jeśli jest to krajobraz, pozwól ekranowi obrócić krajobraz w prawo i w lewo. Jeśli jest to portret, Zablokuj Widok tylko w trybie portretowym.
Najpierw zdefiniuj supportedInterfaceOrientationsForWindow
w AppDelegate.swift
var portrait = true
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
if portrait {
return .Portrait
} else {
return .Landscape
}
}
Po drugie, w głównym kontrolerze widoku zdefiniuj następujące funkcje
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
print("\(#function)")
return .Portrait
}
override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
return .Portrait
}
override func shouldAutorotate() -> Bool {
return false
}
Następnie musisz podklasować AVPlayerViewController
class MyPlayerViewController: AVPlayerViewController {
var size: CGSize?
var supportedOrientationMask: UIInterfaceOrientationMask?
var preferredOrientation: UIInterfaceOrientation?
override func viewDidLoad() {
super.viewDidLoad()
if let size = size {
if size.width > size.height {
self.supportedOrientationMask =[.LandscapeLeft,.LandscapeRight]
self.preferredOrientation =.LandscapeRight
} else {
self.supportedOrientationMask =.Portrait
self.preferredOrientation =.Portrait
}
}
}
ZastÄ…p te trzy funkcje w MyPlayerViewController.swift
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return self.supportedOrientationMask!
}
override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
return self.preferredOrientation!
}
Ponieważ użytkownik może obracać urządzenie w lewo lub w prawo, musimy ustawić automatyczne obracanie, aby było prawdziwe
override func shouldAutorotate() -> Bool {
return true
}
Na koniec utwórz instancję MyPlayerViewController
w kontrolerze widoku i ustaw wartość właściwości size.
let playerViewController = MyPlayerViewController()
// Get the thumbnail
let thumbnail = MyAlbumFileManager.sharedManager().getThumbnailFromMyVideoMedia(......)
let size = thumbnail?.size
playerViewController.size = size
Zainicjuj swojego gracza odpowiednim videoUrl
, a następnie przypisz gracza do playerViewController
. Szczęśliwego kodowania!
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-22 14:14:54
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[UIViewController attemptRotationToDeviceOrientation];
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-20 11:04:27
Zgodnie z odpowiedź Koreya Hintona
Swift 2.2:
extension UINavigationController {
public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return visibleViewController!.supportedInterfaceOrientations()
}
public override func shouldAutorotate() -> Bool {
return visibleViewController!.shouldAutorotate()
}
}
extension UITabBarController {
public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
if let selected = selectedViewController {
return selected.supportedInterfaceOrientations()
}
return super.supportedInterfaceOrientations()
}
public override func shouldAutorotate() -> Bool {
if let selected = selectedViewController {
return selected.shouldAutorotate()
}
return super.shouldAutorotate()
}
}
Wyłącz Rotację
override func shouldAutorotate() -> Bool {
return false
}
Blokada do określonej orientacji
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-22 14:16:40
Próbowałem kilku rozwiązań tutaj i ważne jest, aby zrozumieć, że to kontroler widoku głównego, który określi, czy będzie się obracać, czy nie.
Stworzyłem następujący objective-C projekt github.com/GabLeRoux/RotationLockInTabbedViewChild {[8] } z roboczym przykładem TabbedViewController
, gdzie jeden widok potomny może się obracać, a drugi widok potomny jest zablokowany w pionie.
Nie jest idealny, ale działa i ten sam pomysł powinien działać dla innego rodzaju widoki root, takie jak NavigationViewController
. :)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-01 11:35:04
[iOS9+] Gdyby ktoś tu ciągnął, bo żadne z powyższych rozwiązań nie zadziałało, a jeśli prezentujesz widok, chcesz zmienić orientację do segue ' a, możesz to sprawdzić.
Sprawdź typ prezentacji Twojego segue ' a. Mój był "ponad aktualnym kontekstem". Kiedy zmieniłem go na pełnoekranowy, zadziałało.
Dzięki @ GabLeRoux znalazłem takie rozwiązanie.
- ta zmiana działa tylko w połączeniu z powyższymi rozwiązaniami.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-27 10:20:17
Zgodnie z rozwiązaniem pokazanym przez @ sid-sha musisz umieścić wszystko w metodzie viewDidAppear:
, w przeciwnym razie nie dostaniesz didRotateFromInterfaceOrientation:
, więc coś w stylu:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
NSNumber *value = [NSNumber numberWithInt:interfaceOrientation];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-30 15:01:45
Moje rozwiÄ…zanie
W AppDelegate
:
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
if let topController = UIViewController.topMostViewController() {
if topController is XXViewController {
return [.Portrait, .LandscapeLeft]
}
}
return [.Portrait]
}
XXViewController
jest kontrolerem widoku, który chcesz obsługiwać w trybie poziomym.
Wtedy rozwiązanie Sunny Shah zadziała w twoim XXViewController
na dowolnej wersji iOS:
let value = UIInterfaceOrientation.LandscapeLeft.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
Jest to funkcja użytkowa, która pozwala znaleźć najlepszy Kontroler ViewController.
extension UIViewController {
/// Returns the current application's top most view controller.
public class func topMostViewController() -> UIViewController? {
let rootViewController = UIApplication.sharedApplication().windows.first?.rootViewController
return self.topMostViewControllerOfViewController(rootViewController)
}
/// Returns the top most view controller from given view controller's stack.
class func topMostViewControllerOfViewController(viewController: UIViewController?) -> UIViewController? {
// UITabBarController
if let tabBarController = viewController as? UITabBarController,
let selectedViewController = tabBarController.selectedViewController {
return self.topMostViewControllerOfViewController(selectedViewController)
}
// UINavigationController
if let navigationController = viewController as? UINavigationController,
let visibleViewController = navigationController.visibleViewController {
return self.topMostViewControllerOfViewController(visibleViewController)
}
// presented view controller
if let presentedViewController = viewController?.presentedViewController {
return self.topMostViewControllerOfViewController(presentedViewController)
}
// child view controller
for subview in viewController?.view?.subviews ?? [] {
if let childViewController = subview.nextResponder() as? UIViewController {
return self.topMostViewControllerOfViewController(childViewController)
}
}
return viewController
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:02:46
Użyj tego, aby zablokować orientację kontrolera widoku, testowane na IOS 9:
// Zablokuj orientacjÄ™ w prawo do krajobrazu
-(UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscapeRight;
}
-(NSUInteger)navigationControllerSupportedInterfaceOrientations:(UINavigationController *)navigationController {
return UIInterfaceOrientationMaskLandscapeRight;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-18 05:57:07
Wciąż trwa dyskusja na temat tego, jak najlepiej wykonać to zadanie, więc pomyślałem, że podzielę się moim (roboczym) podejściem. Dodaj następujący kod w swojej implementacji UIViewController
:
- (void) viewWillAppear:(BOOL)animated
{
[UIViewController attemptRotationToDeviceOrientation];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
-(BOOL)shouldAutorotate
{
return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeLeft;
}
W tym przykładzie należy również ustawić dozwolone orientacje urządzenia na "poziomo w lewo" w ustawieniach projektu(lub bezpośrednio w info.plist
). Po prostu zmień orientację, którą chcesz wymusić, jeśli chcesz czegoś innego niż LandscapeLeft.
Kluczem dla mnie było attemptRotationToDeviceOrientation
wezwanie viewWillAppear
- bez tego Widok Nie obracałby się prawidłowo bez fizycznego obracania urządzenia.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-13 02:22:13
Mam ten sam problem i marnuję na niego tyle czasu. Więc teraz mam swoje rozwiązanie. Moje ustawienie aplikacji to tylko obsługa portretu.Jednak niektóre ekrany w mojej aplikacji muszą mieć tylko krajobraz.I fix it by have a variable isShouldRotate at AppDelegate. I funkcja w AppDelegate :
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
if isShouldRotate == true {
return Int(UIInterfaceOrientationMask.Landscape.rawValue)
}
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}
I wreszcie, gdy ViewControllerA wymaga stanu krajobrazu. Po prostu to zrób.: before push / present do viewcontrollera Przypisz powinno się zmienić na true . Nie zapomnij, gdy pop / close ten kontroler przypisać isShouldRotate false at viewWillDisappear.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-22 14:17:26
Dla mnie, najwyższy poziom VC potrzebował zaimplementować przesłonięcia orientacyjne. Używanie VC w dół stosu nie będzie miało wpływu, jeśli top VC nie jest implementacja.
VC-main
|
-> VC 2
|
-> VC 3
Tylko VC-Main jest słuchany, zasadniczo w moich testach.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-26 19:37:13