相关疑难解决方法(0)

获取设备当前方向(App Extension)

如何在App Extension中获取设备当前方向,我尝试了以下两种方法但没有成功.

  1. 它总是返回UIDeviceOrientationUnknown

    [[UIDevice currentDevice] orientation]
    
    Run Code Online (Sandbox Code Playgroud)
  2. 它显示红色消息,'sharedApplication'在iOS上不可用(App Extension)

    [[UIApplication sharedApplication] statusBarOrientation];
    
    Run Code Online (Sandbox Code Playgroud)
  3. 我还添加了一个观察者,但它没有被调用.

    [[NSNotificationCenter defaultCenter] addObserver:self.view selector:@selector(notification_OrientationWillChange:) name:UIApplicationWillChangeStatusBarOrientationNotification object:nil];
    
    
    
    - (void)notification_OrientationWillChange:(NSNotification*)n
    {
       UIInterfaceOrientation orientation = (UIInterfaceOrientation)[[n.userInfo objectForKey:UIApplicationStatusBarOrientationUserInfoKey] intValue];
    
        if (orientation == UIInterfaceOrientationLandscapeLeft)
           [self.textDocumentProxy insertText:@"Left"];
        if (orientation == UIInterfaceOrientationLandscapeRight)
           [self.textDocumentProxy insertText:@"Right"];
    }
    
    Run Code Online (Sandbox Code Playgroud)

那么现在如何才能获得当前的设备定位.

keyboard objective-c ios uideviceorientation ios-app-extension

16
推荐指数
5
解决办法
1万
查看次数

在应用扩展程序中检测方向的最佳方法是什么?

在应用程序扩展中检测设备方向的最佳方法是什么?我在这里发现的解决方案的结果好坏参半:

如何在iOS 8中检测自定义键盘扩展中的方向更改?

获取设备当前方向(App Extension)

我查看了大小类,UITraitCollection并发现设备不准确地报告它是纵向的,实际上是在横向(不确定这是OS错误,还是我没有正确查询正确的API).

完成的最佳方法是什么:

  • 首次加载扩展时设备的当前方向
  • 设备将旋转到的方向
  • 设备旋转的方向

谢谢,

orientation screen-orientation ios ios8 ios-app-extension

12
推荐指数
1
解决办法
7498
查看次数

旋转的视图是否与自动布局兼容?

我学会了如何使用仿射变换旋转视图(参见此处).我还了解了自动布局(参见此处此处),甚至是程序化的自动布局(参见此处此处).但是,我不知道如何让自动布局与旋转视图一起使用.

此图显示了我想要做的事情:

在此输入图像描述

我认为问题来自宽度和高度因旋转而变化.有没有办法让旋转视图填充它的超视图?是否有一些技巧可以使自动布局工作或旋转后它是不兼容的?

(我只学过Swift,但如果你更熟悉的话,我会很高兴通过Objective-C答案.)

更新

按照@ VinayJain的建议,我做了以下事情:

  • 将子视图边缘固定到故事板中的superview.
  • 为子视图的所有边上的间距约束创建了IBOutlets.

    @IBOutlet weak var rightSpace: NSLayoutConstraint!
    @IBOutlet weak var leftSpace: NSLayoutConstraint!
    @IBOutlet weak var topSpace: NSLayoutConstraint!
    @IBOutlet weak var bottomSpace: NSLayoutConstraint!
    
    Run Code Online (Sandbox Code Playgroud)
  • 旋转子视图

    subview.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))
    
    Run Code Online (Sandbox Code Playgroud)
  • 改变了网点的限制

    self.rightSpace.constant = CGFloat(0)
    self.leftSpace.constant = CGFloat(0)
    self.topSpace.constant = CGFloat(0)
    self.bottomSpace.constant = CGFloat(0)
    
    Run Code Online (Sandbox Code Playgroud)

但就在这一点上,我意识到我并不需要改变间距值.我希望间距保持为0.我只需要它来调整自己.然而,旋转混乱了.效果如下图所示:

在此输入图像描述

rotation ios autolayout

10
推荐指数
1
解决办法
1967
查看次数