我正在开发一款适用于iPhone的快速应用程序.在我的应用程序中有一个模态视图,我只想在纵向视图中.
我的问题是,我如何以编程方式强制手机不允许旋转?换句话说,我正在寻找不允许以横向模式显示模态视图的代码(打开纵向旋转锁定).
这仅适用于1个模态视图,因此我无法关闭整个应用程序的旋转,否则我只会完全禁用旋转.
我在这里的研究中找到了代码 但是在目标C中,如果有帮助的话.谢谢!
我使用swift为iPhone创建了一个应用程序,它由嵌入在导航控制器中的许多视图组成.我想将主视图锁定为纵向方向,并且仅锁定横向方向的导航控制器的子视图.这是我的意思的一个例子:
我怎样才能做到这一点?
我完成了我的iOS应用程序,但我只需要将一个视图设置为横向模式,其余视图只能在纵向模式下查看.
我正在使用Xcode 5.1,我通过从右侧面板中删除我的故事板视图控制器来创建我的所有视图,所以如果你要告诉我在某处编写一些代码,请告诉我我需要编写它的确切位置.
我在这里阅读了一个解决方案UINavigationController Force Rotate但我不知道在哪里编写该代码.我需要UIViewController手动创建一个吗?
当我尝试在控制器上仅允许纵向方向时,我总是收到此错误:Error Domain=UISceneErrorDomain Code=101“视图控制器不支持所请求的方向。请求:landscapeLeft;支持:纵向”UserInfo={NSLocalizedDescription=视图控制器不支持所请求的方向。请求:景观左;支持:纵向}
我称这个方法为:
func updateOrientation(orientation: UIInterfaceOrientationMask) {
if #available(iOS 16, *) {
DispatchQueue.main.async {
let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
self.setNeedsUpdateOfSupportedInterfaceOrientations()
self.navigationController?.setNeedsUpdateOfSupportedInterfaceOrientations()
windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: orientation)) { error in
print(error)
print(windowScene?.effectiveGeometry )
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
有人面临同样的问题吗?
我有一个 SwiftUI 项目。对于除一种视图之外的所有视图,我想允许纵向和仅纵向模式。对于只有一种视图,我想同时允许纵向和横向。Swift 上有一些资源,但我在 SwiftUI 上找不到任何资源。
有没有人找到一种方法来实现这一点?
我正在创建一个仅支持纵向模式的应用程序.我不希望风景或人像颠倒.我尝试了一些代码.这允许我锁定纵向模式.
我正在使用navigationcontroller和presentviewcontroller.现在我的问题是:1.如果我将设备颠倒并打开我的应用程序,它会以颠倒模式打开,这是错误的.2.我点击一些按钮,进入presentviewcontroller,返回纵向模式.
我希望所有的navigationcontroller和presentviewcontroller都处于纵向模式
我的代码:
我设置了设备orientarion肖像 Target -> General -> Deployment Info -> Portrait
在我的appdelagate.swift中:
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
}
Run Code Online (Sandbox Code Playgroud)
在我的第一个视图控制器.这基本上是导航控制器的孩子
override func shouldAutorotate() -> Bool {
return false
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return [UIInterfaceOrientationMask.Portrait ]
}
Run Code Online (Sandbox Code Playgroud)
编辑1:
我也定了 StoryBoard -> ViewController -> Attribute Inspector -> Orientation -> Portrait
编辑2:
背景
我有一个使用AVFoundation的应用程序以拥有自定义相机。这发生在中OCRViewController。当我拍照时,会将捕获的图片发送到其他视图ImagePreviewViewController。
我使用Xcode 10.2.1 (10E1001)与Swift 5
目标
我想实现的是将的方向锁定ImagePreviewViewController为图像的原始方向。我已经知道如何获取图像的方向,但是我无法锁定视图的方向。
我得到这样的图像旋转: let imageOri = capturedImage?.imageOrientation
我尝试了什么?
我尝试了其他来源的公认答案:
在https://developer.apple.com/documentation/uikit/uiviewcontroller#//apple_ref/occ/instm/UIViewController/supportedInterfaceOrientations下的“ 处理视图旋转 ” 下阅读文档,内容如下:
在编写此查询时,我还尝试了许多建议的解决方案,但是,大多数方法似乎使用以下方法(或它的一种变体),它对我不起作用。
override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}
override func shouldAutorotate() -> Bool{
return false
}
override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
return UIInterfaceOrientation.Portrait
}
Run Code Online (Sandbox Code Playgroud)
从iOS 8开始,不推荐使用所有与旋转相关的方法。相反,旋转被视为视图控制器视图大小的变化,因此使用viewWillTransition(to:with :)方法进行报告。
但是,我不确定如何从这里开始。
有趣的代码片段
我的中使用了以下方法OCRViewController,在此实例化ImagePreviewViewController并附加捕获的图像。
func displayCapturedPhoto(capturedPhoto : UIImage) {
let imagePreviewViewController = storyboard?.instantiateViewController(withIdentifier: "ImagePreviewViewController") …Run Code Online (Sandbox Code Playgroud) 大家好我正在使用此代码stackoverflow
例如,我有视图1(仅限肖像)和视图2(纵向+横向).当您在视图1中并单击按钮时,在整个屏幕上打开视图2并弹出窗口,当您关闭视图2并且视图1变为可见时,如果视图2处于横向状态,我希望自动将其转换为纵向模式.有什么建议 ?
更新2
在我的UITabBarController子类中,我尝试添加这个:
-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}
Run Code Online (Sandbox Code Playgroud)
现在,每次我选择一个标签项时,设备都会完美地旋转到纵向模式.但是,现在我可以在选择(a)时旋转设备,设备将旋转到横向模式.如何阻止设备旋转?
我相信我的tab控制器子类中的这个方法是导致这种情况的原因:
- (BOOL)shouldAutorotate {
if(self.selectedIndex == 0)
{
return NO;
}
return [self.viewControllers.lastObject shouldAutorotate];
}
Run Code Online (Sandbox Code Playgroud)
如果我返回"否",当我在视图控制器中时,我无法旋转,但是当我选择它时,它不会自动旋转为纵向.如果我返回"是",我可以在视图控制器中旋转,但是当我选择它时,它会自动旋转为纵向.
我的应用程序中有一个自定义标签栏控制器,具有以下层次结构:
UITabBarController
|
UINavigationController
| |
| UIViewController(a)
|
UINavigationController
| |
| UIViewController(b)
|
UINavigationController
|
UIViewController(c)
Run Code Online (Sandbox Code Playgroud)
我希望视图控制器(a)只能在纵向模式下查看,而视图控制器(b)和(c)可以在所有方向上查看,但是可以上下颠倒.现在,我可以单独为每个视图控制器执行此操作,但是当我处于横向模式(b)并选择(a)的选项卡项时,我的问题就出现了,然后以横向模式显示a,这看起来不像好.如何确保标签栏(或视图控制器)检查是否可以在当前方向查看要选择的视图控制器?
如果需要,这是我的代码(a)将其自身限制为纵向模式:
- (BOOL) shouldAutorotate
{
return NO;
}
- (NSUInteger) supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
Run Code Online (Sandbox Code Playgroud)
UPDATE
我已将其添加到视图控制器(a)中,但我在视图中间得到一个黑色方块,导航栏中的标题不再居中.
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait]; …Run Code Online (Sandbox Code Playgroud) objective-c rotation uitabbarcontroller uiviewcontroller ios
我有一个LaunchScreen.storybaord显示徽标的徽标(文字格式,因此与方向无关)。该应用程序始终以纵向启动,但是它具有允许横向模式的某些视图控制器(因此,不可以仅使应用程序为纵向)。
我想要的是启动屏幕始终显示为纵向。因此,在应用启动过程中将手机保持在横向模式下应该会导致徽标旋转(就像在查看不支持横向模式的视图控制器一样)
有没有办法告诉情节提要只能是纵向的?
我尝试使用大小类特征,但是这种方式只能解决左或右横向问题。由于还为初次用户提供了一个启动屏幕的徽标屏幕,因此徽标旋转取决于手机在横向放置的方向。
我可以在一个控制器中设置纵向和锁定方向吗?
我试过了:
override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}
override func shouldAutorotate() -> Bool{
return false
}
Run Code Online (Sandbox Code Playgroud)
但它对我没有帮助.我必须添加其他东西吗?
我有一个应用程序,我仅在其目标设置中将方向设置为纵向:
在一个特定的视图控制器中,我想覆盖此设置,以便在设备旋转时自动布局将更新视图。我试过这些方法没有成功:
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.AllButUpsideDown
}
Run Code Online (Sandbox Code Playgroud) ios ×9
swift ×7
orientation ×3
portrait ×3
iphone ×2
landscape ×2
objective-c ×2
rotation ×2
xcode ×2
autolayout ×1
ios10 ×1
ios16 ×1
ios8 ×1
swift5 ×1
swiftui ×1
uistoryboard ×1
xcode14 ×1