首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >@选择器的objc_msgSend (setOrientation:)

@选择器的objc_msgSend (setOrientation:)
EN

Stack Overflow用户
提问于 2014-10-14 16:56:29
回答 1查看 250关注 0票数 1

在我的应用程序中,我使用objc_msgSend来改变整个设备的方向。在应用程序中有2个按钮(横向和纵向)。每当用户点击景观,那么应用程序应该是在肖像landscape.Similar。

但是这种方法不会每次都改变方向。它有时有效,有时无效。

代码是:-

代码语言:javascript
复制
- (void)updateOrientation
{
    NSInteger mode = -1;
    switch (appDel.orientationMode)
    {
        case 2:
        {
            UIDeviceOrientation orientation =  (UIDeviceOrientation)[UIApplication sharedApplication].statusBarOrientation;
            if (orientation == UIDeviceOrientationLandscapeLeft ||
                    orientation == UIDeviceOrientationLandscapeRight
                    || orientation == 0)
            {
                // do nothing
            }
            else if(orientation == UIDeviceOrientationPortrait) {

                mode = UIInterfaceOrientationLandscapeRight ;
            }
            else if (orientation == UIDeviceOrientationPortraitUpsideDown ) {
                mode = UIInterfaceOrientationLandscapeLeft;
            }
        }
            break;
        case 3:
        {
            UIDeviceOrientation orientation =  (UIDeviceOrientation)[UIApplication sharedApplication].statusBarOrientation;

            if (orientation == UIDeviceOrientationPortrait ||
                orientation == UIDeviceOrientationPortraitUpsideDown
                || orientation == 0)
            {
                // do nothing
            }
            else if (orientation == UIDeviceOrientationLandscapeRight) {

                mode = UIInterfaceOrientationPortraitUpsideDown ;
            }
            else if (orientation == UIDeviceOrientationLandscapeLeft) {

                 mode = UIInterfaceOrientationPortrait ;
            }
        }
            break;
    }

    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)] && (mode != -1))
    {
        objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), mode);
    }
}
EN

回答 1

Stack Overflow用户

发布于 2015-02-16 18:27:15

当您更改方向时,请尝试此代码

代码语言:javascript
复制
NSNumber *value = [NSNumber numberWithInt:YourDesiredOrientation];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26356551

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档