是否可以 Rotate/Affine 改造 UIMenuController

Is it possible to Rotate/Affine Transform UIMenuController

无需深入 'why',我有 UITextField,它是视图层次结构的一部分,通过仿射变换在屏幕上强制 'upside down'。

CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(180))

这适用于所有子视图——包括 UITextField——直到用户想要在文本字段内容上使用 UIMenuController 的 copy/paste 功能。当显示 UIMenuController 时,它是 'right side up' 而不是像 UITextField 那样的 'upside down'。

有没有办法获取 UIMenuController 的视图以在显示时应用相同的转换?

目前,我正在监听 UIMenuControllerWillShowMenuNotification 通知,然后获取 UIMenuController。但我似乎找不到将转换应用于它的方法。有任何想法吗?

- (void)textFieldDidBeginEditing:(UITextField *)textField {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuControllerWillShow:) name:UIMenuControllerWillShowMenuNotification object:nil];

}

- (void)menuControllerWillShow:(NSNotification*)aNotification {

    UIMenuController* menuController = [UIMenuController sharedMenuController];
    CGAffineTransform xform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(180));

    CGRect oldRect = menuController.menuFrame;
    CGRect newRect = CGRectApplyAffineTransform(oldRect, xform);   

    // eh... now what? 
}

我不认为你可以修改 UIMenuController 所以它颠倒了。

但如果几乎整个应用都发生了变化,您可以检查设备方向或应用方向(通过检查状态栏的位置)并强制使其相反。

要获得 device orientation 你可以这样做:

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]

文档注释:

The value of this property always returns 0 unless orientation notifications have been enabled by calling beginGeneratingDeviceOrientationNotifications.

或通过以下方式获取应用程序方向:

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

if(orientation == 0) //Default orientation 
{
    //UI is in Default (Portrait) -- this is really a just a failsafe. 
}
else if(orientation == UIInterfaceOrientationPortrait)
{ 
    //Do something if the orientation is in Portrait
}
else if(orientation == UIInterfaceOrientationLandscapeLeft)
{     
   // Do something if Left
}
else if(orientation == UIInterfaceOrientationLandscapeRight)
{    
    //Do something if right
}

然后像这样设置它:

Objective-C:

NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];

Swift:

let value = UIInterfaceOrientation.LandscapeLeft.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")