我从Cocoa Controls下载了一个使用此方法的simpleCamera视图
- (AVCaptureVideoOrientation)orientationForConnection
{
AVCaptureVideoOrientation videoOrientation = AVCaptureVideoOrientationPortrait;
switch (self.interfaceOrientation) {
case UIInterfaceOrientationLandscapeLeft:
videoOrientation = AVCaptureVideoOrientationLandscapeLeft;
break;
case UIInterfaceOrientationLandscapeRight:
videoOrientation = AVCaptureVideoOrientationLandscapeRight;
break;
case UIInterfaceOrientationPortraitUpsideDown:
videoOrientation = AVCaptureVideoOrientationPortraitUpsideDown;
break;
default:
videoOrientation = AVCaptureVideoOrientationPortrait;
break;
}
return videoOrientation;
}
问题是在iOS 8中不推荐使用“interfaceOrientation”但我不知道在Switch条件中替换它的方法和方法。
答案 0 :(得分:141)
获取设备方向不正确。例如,设备可能处于横向模式,但仅支持纵向的视图控制器将保留为纵向。
相反,请使用:
void checkData()
{
SuspendLayout();
try
{
MySqlConnection conn = new MySqlConnection(myConnection);
conn.Open();
MySqlCommand command = new MySqlCommand("SELECT * FROM maindatabase.animelist where TitleAnime=?Title;", conn);
//command.Parameters.AddWithValue("?CN", int.Parse(a.ToString()));
command.Parameters.AddWithValue("?Title", textBox3.Text);
MySqlDataReader reader = command.ExecuteReader();
int ctr = 0;
while (reader.Read())
{
ctr++;
}
if (ctr == 1)
{
my = Form.ActiveForm as MyList;
my.msg = new Message_Box();
my.msg.Descrip.Text = "Record is already in the Database";
my.msg.Title.Text = "Duplicate Record";
my.msg.ShowDialog();
}
else
{
updateData();
}
conn.Close();
ResumeLayout();
}
catch (Exception ex)
{
MessageBox.Show("" + ex);
}
}
另一个好处是它仍然使用UIInterfaceOrientation枚举,因此您的代码很少需要更改。
答案 1 :(得分:19)
从iOS8开始,Apple建议使用TraitCollections(Size Classes)而不是interfaceOrientation。
此外,由于iOS 9和新iPad功能"多任务",在某些情况下设备方向与窗口比例不相符! (打破你的应用程序用户界面)
所以你在使用普通尺寸课时也应该非常小心,因为它不需要占用整个iPad屏幕。
有时,TraitCollections并不能满足您的所有设计需求。对于这些情况,Apple建议比较视图的界限:
if view.bounds.size.width > view.bounds.size.height {
// ...
}
我很惊讶,但您可以在21 15上查看WWDC 2015视频Getting Started with Multitasking on iPad in iOS 9。
也许你真的在寻找设备方向而不是屏幕或窗口比例。如果您关心设备摄像头,则不应使用TraitCollection,也不应使用视图边界。
答案 2 :(得分:2)
使用UIDevice的-orientation
属性不正确(即使它可以在大多数情况下工作)并且可能导致一些错误,例如UIDeviceOrientation
也考虑设备的方向(如果是面朝上或朝下,UIInterfaceOrientation
枚举中没有对这些值的对
此外,如果您将应用程序锁定在某个特定方向,UIDevice将为您提供设备方向,而不考虑这一点
另一方面,iOS8已弃用interfaceOrientation
类的UIViewController
属性。
有两个选项可用于检测界面方向:
仍然缺少一种理解界面方向变化方向的方法,这在动画制作过程中非常重要。
在WWDC 2014的会议中"查看iOS8中的控制器进展"说话者也使用替换-will/DidRotateToInterfaceOrientation
的方法提供了解决该问题的方法。
这里提出的解决方案部分实施:
-(void) viewWillTransitionToSize:(CGSize)s withTransitionCoordinator:(UIVCTC)t {
orientation = [self orientationFromTransform: [t targetTransform]];
oldOrientation = [[UIApplication sharedApplication] statusBarOrientation];
[self myWillRotateToInterfaceOrientation:orientation duration: duration];
[t animateAlongsideTransition:^(id <UIVCTCContext>) {
[self myWillAnimateRotationToInterfaceOrientation:orientation
duration:duration];
}
completion: ^(id <UIVCTCContext>) {
[self myDidAnimateFromInterfaceOrientation:oldOrientation];
}];
}
答案 3 :(得分:1)
Swift 4+版
UIApplication.shared.statusBarOrientation
答案 4 :(得分:0)
使用UIApplication.shared.statusBarOrientation
时,Xcode 11将显示警告'statusBarOrientation' was deprecated in iOS 13.0: Use the interfaceOrientation property of the window scene instead.
此答案在Swift 5中,并且支持iOS 13和更低版本。它假定以下条件:
previewLayer
的成员变量AVCaptureVideoPreviewLayer
首先,在您的SceneDelegate.swift
中,添加以下几行:
static var lastCreatedScene: UIWindowScene?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let newScene = (scene as? UIWindowScene) else { return }
Self.lastCreatedScene = newScene
}
然后在您的视图中,添加以下函数并在layoutSubviews()
中调用它:
func refreshOrientation() {
let videoOrientation: AVCaptureVideoOrientation
let statusBarOrientation: UIInterfaceOrientation
if #available(iOS 13, *) {
statusBarOrientation = SceneDelegate.lastCreatedScene?.interfaceOrientation ?? .portrait
} else {
statusBarOrientation = UIApplication.shared.statusBarOrientation
}
switch statusBarOrientation {
case .unknown:
videoOrientation = .portrait
case .portrait:
videoOrientation = .portrait
case .portraitUpsideDown:
videoOrientation = .portraitUpsideDown
case .landscapeLeft:
videoOrientation = .landscapeLeft
case .landscapeRight:
videoOrientation = .landscapeRight
@unknown default:
videoOrientation = .portrait
}
self.previewLayer.connection?.videoOrientation = videoOrientation
}