设备方向更改后窗口子视图不旋转

发布于 2024-12-16 15:35:36 字数 4913 浏览 3 评论 0原文

我正在 AppDelegate 中创建一个带有标签的 UIView 并显示它,如下所示:

    [window addSubview:self.roundedCornerView];

问题是当我旋转设备时,带有标签的视图根本不旋转。标签中的文字方向也错误。我的应用程序中的窗口有另一个子视图,即 UIViewControllers 子视图,并且它旋转得很好。

我是否需要在 AppDelegate 中创建另一个 UIViewController 并将创建的视图附加到它,然后将其子类化并允许界面方向以使 roundedCornerView 旋转?

更新 好的,我尝试通过创建新的 ViewController 并对其进行子继承来实现此目的,这里是我的 AppDelegate 中的代码:

    ActivityIndicatorWithLabelViewController *aiWithLabel = [[[ActivityIndicatorWithLabelViewController alloc] init] autorelease];
aiWithLabel.textOfTheLabel = text;

[window addSubview:aiWithLabel.view];

ActivityIndi​​catorWithLabelViewController 类在此处可见:

//
//  ActivityIndicatorWithLabelViewController.m
//  LOFT
//
//  Created by Marcin Zyga on 15.11.2011.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "ActivityIndicatorWithLabelViewController.h"

@implementation ActivityIndicatorWithLabelViewController
@synthesize roundedCornerView;
@synthesize textActivityIndicatorLabel;
@synthesize textOfTheLabel;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];

    UIActivityIndicatorView *mainApplicationActivityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    mainApplicationActivityIndicatorView.frame = CGRectMake(80, 80, 40, 40);
    mainApplicationActivityIndicatorView.hidesWhenStopped = YES;


    //self.roundedCornerView = [[[UIView alloc] initWithFrame:CGRectMake(280, 400, 200, 200)] autorelease];
    self.roundedCornerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)] autorelease];
    roundedCornerView.backgroundColor = [UIColor blackColor];
    roundedCornerView.alpha = 0.9f;
    roundedCornerView.layer.cornerRadius = 12.0;
    [roundedCornerView addSubview:mainApplicationActivityIndicatorView];


    [mainApplicationActivityIndicatorView startAnimating];
    //  self.roundedCornerView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
    //self.roundedCornerView.autoresizesSubviews = YES;



    self.textActivityIndicatorLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 150, 200, 50)];
    self.textActivityIndicatorLabel.backgroundColor = [UIColor clearColor];
    self.textActivityIndicatorLabel.textAlignment = UITextAlignmentCenter;
    self.textActivityIndicatorLabel.textColor = [UIColor whiteColor];
    self.textActivityIndicatorLabel.font = [UIFont systemFontOfSize:22];
    self.textActivityIndicatorLabel.text = @"";
    //  self.textActivityIndicatorLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth; 
    [self.roundedCornerView addSubview:textActivityIndicatorLabel]; 
    self.textActivityIndicatorLabel.text = textOfTheLabel;

    self.view.frame = CGRectMake(280, 400, 200, 200);
    [self.view addSubview:self.roundedCornerView];
    //self.view = self.roundedCornerView;

}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
        [self.textActivityIndicatorLabel removeFromSuperview];
        [self.textActivityIndicatorLabel release];

        self.textActivityIndicatorLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
        self.textActivityIndicatorLabel.backgroundColor = [UIColor clearColor];
        self.textActivityIndicatorLabel.textAlignment = UITextAlignmentCenter;
        self.textActivityIndicatorLabel.textColor = [UIColor whiteColor];
        self.textActivityIndicatorLabel.font = [UIFont systemFontOfSize:22];
        self.textActivityIndicatorLabel.text = @"Landscape";
        [self.roundedCornerView addSubview:textActivityIndicatorLabel];         

        NSLog(@"LANDSCAPE");
    }
    NSLog(@"ENTERING SUPPORTED ORIENTATION!");
    return YES;
}

@end

如您所见,这里有一些调试代码。当我将设备从肖像旋转到风景时,我进入了支撑方向!以及 LADNSCAPE NSLog。删除标签工作正常,但是当我添加新标签时,它仍然以错误的方向显示(文本)。我做错了什么?

I'm creating an UIView with a label inside AppDelegate and displaying it like this:

    [window addSubview:self.roundedCornerView];

Problem is when I rotate the device the view with label don't rotate at all. The text in the label is in wrong orientation as well. Window in my application got another subview which is the UIViewControllers subview and it is rotating fine.

Do I need to create another UIViewController in my AppDelegate and attach created view to it, then subclassing it and allowing for interface orientation in order to get roundedCornerView to rotate?

UPDATE
Ok I've tried to do this by creating new ViewController and sublcassing it here is code in my AppDelegate:

    ActivityIndicatorWithLabelViewController *aiWithLabel = [[[ActivityIndicatorWithLabelViewController alloc] init] autorelease];
aiWithLabel.textOfTheLabel = text;

[window addSubview:aiWithLabel.view];

The ActivityIndicatorWithLabelViewController class is visible here:

//
//  ActivityIndicatorWithLabelViewController.m
//  LOFT
//
//  Created by Marcin Zyga on 15.11.2011.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "ActivityIndicatorWithLabelViewController.h"

@implementation ActivityIndicatorWithLabelViewController
@synthesize roundedCornerView;
@synthesize textActivityIndicatorLabel;
@synthesize textOfTheLabel;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];

    UIActivityIndicatorView *mainApplicationActivityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    mainApplicationActivityIndicatorView.frame = CGRectMake(80, 80, 40, 40);
    mainApplicationActivityIndicatorView.hidesWhenStopped = YES;


    //self.roundedCornerView = [[[UIView alloc] initWithFrame:CGRectMake(280, 400, 200, 200)] autorelease];
    self.roundedCornerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)] autorelease];
    roundedCornerView.backgroundColor = [UIColor blackColor];
    roundedCornerView.alpha = 0.9f;
    roundedCornerView.layer.cornerRadius = 12.0;
    [roundedCornerView addSubview:mainApplicationActivityIndicatorView];


    [mainApplicationActivityIndicatorView startAnimating];
    //  self.roundedCornerView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
    //self.roundedCornerView.autoresizesSubviews = YES;



    self.textActivityIndicatorLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 150, 200, 50)];
    self.textActivityIndicatorLabel.backgroundColor = [UIColor clearColor];
    self.textActivityIndicatorLabel.textAlignment = UITextAlignmentCenter;
    self.textActivityIndicatorLabel.textColor = [UIColor whiteColor];
    self.textActivityIndicatorLabel.font = [UIFont systemFontOfSize:22];
    self.textActivityIndicatorLabel.text = @"";
    //  self.textActivityIndicatorLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth; 
    [self.roundedCornerView addSubview:textActivityIndicatorLabel]; 
    self.textActivityIndicatorLabel.text = textOfTheLabel;

    self.view.frame = CGRectMake(280, 400, 200, 200);
    [self.view addSubview:self.roundedCornerView];
    //self.view = self.roundedCornerView;

}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
        [self.textActivityIndicatorLabel removeFromSuperview];
        [self.textActivityIndicatorLabel release];

        self.textActivityIndicatorLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
        self.textActivityIndicatorLabel.backgroundColor = [UIColor clearColor];
        self.textActivityIndicatorLabel.textAlignment = UITextAlignmentCenter;
        self.textActivityIndicatorLabel.textColor = [UIColor whiteColor];
        self.textActivityIndicatorLabel.font = [UIFont systemFontOfSize:22];
        self.textActivityIndicatorLabel.text = @"Landscape";
        [self.roundedCornerView addSubview:textActivityIndicatorLabel];         

        NSLog(@"LANDSCAPE");
    }
    NSLog(@"ENTERING SUPPORTED ORIENTATION!");
    return YES;
}

@end

As you see there is some debug code in here. When I'm rotating the device from potrait to landscape I get ENTERING SUPPORTE ORIENTATION! as well as LADNSCAPE NSLog. Removing label is working fine, but when I'm adding new one it is still presented (the text) in wrong orientation. What I am doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吃不饱 2024-12-23 15:35:36

UIWindow 应该只有一个定义根 UIViewController 的子视图。我相信 UIWindow 仅将旋转事件转发到其第一个子视图。

创建一个容器 UIView 并将子视图移入其中。

UIWindow should only have one subview which defines the root UIViewController. I believe that UIWindow only forwards rotation events to its first subview.

Create a single container UIView and move your subviews into it.

为你鎻心 2024-12-23 15:35:36

解决方案遵循以下步骤

  • 在子视图的.m文件中,在视图界面顶部添加代码:

    typedef NS_OPTIONS(NSUInteger, AGInterfaceOrientationMask) {
      AGInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
      AGInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
      AGInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
      AGInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
      AGInterfaceOrientationMaskLandscape = (AGInterfaceOrientationMaskLandscapeLeft | AGInterfaceOrientationMaskLandscapeRight),
      AGInterfaceOrientationMaskAll = (AGInterfaceOrientationMaskPortrait | AGInterfaceOrientationMaskLandscapeLeft | AGInterfaceOrientationMaskLandscapeRight | AGInterfaceOrientationMaskPortraitUpsideDown),
      AGInterfaceOrientationMaskAllButUpsideDown = (AGInterfaceOrientationMaskPortrait | AGInterfaceOrientationMaskLandscapeLeft | AGInterfaceOrientationMaskLandscapeRight),
    }
    
  • 在 subView 的 .m 文件中添加代码

    #pragma mark - 方向
    
    - (void)statusBarFrameOrOrientationChanged:(NSNotification *)通知
    {
      /*
      此通知很可能在动画块内触发,
      因此不需要动画来执行这个漂亮的过渡。
      */
      [自我旋转根据状态栏方向和支持的方向];
    }
    
    - (void)rotateAccordingToStatusBarOrientationAndSupportedOrientations
    {
        UIInterfaceOrientation 方向 = [selfdesiredOrientation];
        CGFloat角度= [self UIInterfaceOrientationAngleOfOrientation:方向];
        CGFloat statusBarHeight = [[自身类] getStatusBarHeight];
        UIInterfaceOrientation statusBarOrientation = [UIApplication共享应用程序].statusBarOrientation;
    
        CGAffineTransform 变换 = CGAffineTransformMakeRotation(角度);
        CGRect框架= [[自身类] rectInWindowBounds:self.view.window.bounds statusBarOrientation:statusBarOrientation statusBarHeight:statusBarHeight];
    
        [self setIfNotEqualTransform:变换帧:帧];
    
        CGRect 矩形 = btnClose.frame;
        矩形.origin.x = (subView.frame.origin.x+subView.frame.size.width)-(矩形.size.width/2);
        rect.origin.y = subView.frame.origin.y-(rect.size.height/2);
        btnClose.frame = 矩形;
    }
    
    - (void)setIfNotEqualTransform:(CGAffineTransform)变换框架:(CGRect)框架
    {
        if(!CGAffineTransformEqualToTransform(self.view.transform, 变换))
        {
            self.view.transform = 变换;
        }
        if(!CGRectEqualToRect(self.view.frame,frame))
        {
            self.view.frame = 框架;
        }
    }
    
    + (CGFloat)获取StatusBarHeight
    {
        UIInterfaceOrientation 方向 = [UIApplication共享应用程序].statusBarOrientation;
        if(UIInterfaceOrientationIsLandscape(方向))
        {
            返回[UIApplication共享应用].statusBarFrame.size.width;
        }
        别的
        {
            返回[UIApplication共享应用].statusBarFrame.size.height;
        }
    }
    
    静态 BOOL IS_BELOW_IOS_7()
    {
        静态 BOOL 答案;
        静态dispatch_once_t OnceToken;
        dispatch_once(&onceToken, ^{
            答案= [[[UIDevice currentDevice] systemVersion] floatValue] < 7.0;
        });
        返回答案;
    }
    
    + (CGRect)rectInWindowBounds:(CGRect)windowBounds statusBarOrientation:(UIInterfaceOrientation)statusBarOrientation statusBarHeight:(CGFloat)statusBarHeight
    {
        CGRect 框架 = windowBounds;
    
        如果(IS_BELOW_IOS_7())
        {
            frame.origin.x += statusBarOrientation == UIInterfaceOrientationLandscapeLeft ?状态栏高度:0;
            frame.origin.y += statusBarOrientation == UIInterfaceOrientationPortrait ?状态栏高度:0;
            frame.size.width -= UIInterfaceOrientationIsLandscape(statusBarOrientation) ?状态栏高度:0;
            frame.size.height -= UIInterfaceOrientationIsPortrait(statusBarOrientation) ?状态栏高度:0;
        }
        返回帧;
    }
    
    - (UIInterfaceOrientation)desiredOrientation
    {
        UIInterfaceOrientation statusBarOrientation = [[UIApplication共享应用] statusBarOrientation];
        AGInterfaceOrientationMask statusBarOrientationAsMask = [self AGInterfaceOrientationMaskFromOrientation:statusBarOrientation];
        if(self.supportedInterfaceOrientations & statusBarOrientationAsMask)
        {
            返回状态栏方向;
        }
        别的
        {
            if(self.supportedInterfaceOrientations & AGInterfaceOrientationMaskPortrait)
            {
                返回 UIInterfaceOrientationPortrait;
            }
            else if(self.supportedInterfaceOrientations & AGInterfaceOrientationMaskLandscapeLeft)
            {
                返回UIInterfaceOrientationLandscapeLeft;
            }
            else if(self.supportedInterfaceOrientations & AGInterfaceOrientationMaskLandscapeRight)
            {
                返回UIInterfaceOrientationLandscapeRight;
            }
            别的
            {
                返回UIInterfaceOrientationPortraitUpsideDown;
            }
        }
    }
    
    -(CGFloat)UIInterfaceOrientationAngleOfOrientation:(UIInterfaceOrientation)方向
    {
        CG浮角;
    
        开关(方向)
        {
            案例 UIInterfaceOrientationPortraitUpsideDown:
                角度=M_PI;
                休息;
            案例UIInterfaceOrientationLandscapeLeft:
                角度=-M_PI_2;
                休息;
            案例UIInterfaceOrientationLandscapeRight:
                角度=M_PI_2;
                休息;
            默认:
                角度=0.0;
                休息;
        }
    
        返回角;
    }
    
    -(AGInterfaceOrientationMask)AGInterfaceOrientationMaskFromOrientation:(UIInterfaceOrientation)方向
    {
        返回 1 <<方向;
    }
    
    //如果dealloc是重复的方法,则删除此dealloc方法并在dealloc中添加unregisterFromNotifications方法。
    - (void)dealloc {
        [自我取消注册通知];
    }
    
    -(void)unregisterFromNotifications
    {
        //用于定向
        [[NSNotificationCenterdefaultCenter]removeObserver:self 名称:UIApplicationDidChangeStatusBarOrientationNotification 对象:nil];
        [[NSNotificationCenterdefaultCenter]removeObserver:self 名称:UIApplicationDidChangeStatusBarFrameNotification 对象:nil];
    }
    
  • 另外在 subView 的 .h 文件中添加此行。

    - (void)rotateAccordingToStatusBarOrientationAndSupportedOrientations;
    

现在是时候使用上面添加的代码来更改子视图中的方向了。

  • 在 window 或 self.view 中添加子视图时,如下所示:

    [objAppDelegate.window addSubview:objViewController.view];
    
    //添加此方法以在添加时根据方向旋转子视图
    [objViewControllerrotateAccordingToStatusBarOrientationAndSupportedOrientations];
    

参考取自 AGWindowView

Solution is follow below steps :

  • In subView's .m file add code on top of interface of you view:

    typedef NS_OPTIONS(NSUInteger, AGInterfaceOrientationMask) {
      AGInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
      AGInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
      AGInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
      AGInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
      AGInterfaceOrientationMaskLandscape = (AGInterfaceOrientationMaskLandscapeLeft | AGInterfaceOrientationMaskLandscapeRight),
      AGInterfaceOrientationMaskAll = (AGInterfaceOrientationMaskPortrait | AGInterfaceOrientationMaskLandscapeLeft | AGInterfaceOrientationMaskLandscapeRight | AGInterfaceOrientationMaskPortraitUpsideDown),
      AGInterfaceOrientationMaskAllButUpsideDown = (AGInterfaceOrientationMaskPortrait | AGInterfaceOrientationMaskLandscapeLeft | AGInterfaceOrientationMaskLandscapeRight),
    }
    
  • In subView's .m file add code

    #pragma mark - Orientation
    
    - (void)statusBarFrameOrOrientationChanged:(NSNotification *)notification
    {
      /*
      This notification is most likely triggered inside an animation block,
      therefore no animation is needed to perform this nice transition.
      */
      [self rotateAccordingToStatusBarOrientationAndSupportedOrientations];
    }
    
    - (void)rotateAccordingToStatusBarOrientationAndSupportedOrientations
    {
        UIInterfaceOrientation orientation = [self desiredOrientation];
        CGFloat angle = [self UIInterfaceOrientationAngleOfOrientation:orientation];
        CGFloat statusBarHeight = [[self class] getStatusBarHeight];
        UIInterfaceOrientation statusBarOrientation = [UIApplication sharedApplication].statusBarOrientation;
    
        CGAffineTransform transform = CGAffineTransformMakeRotation(angle);
        CGRect frame = [[self class] rectInWindowBounds:self.view.window.bounds statusBarOrientation:statusBarOrientation statusBarHeight:statusBarHeight];
    
        [self setIfNotEqualTransform:transform frame:frame];
    
        CGRect rect = btnClose.frame;
        rect.origin.x = (subView.frame.origin.x+subView.frame.size.width)-(rect.size.width/2);
        rect.origin.y = subView.frame.origin.y-(rect.size.height/2);
        btnClose.frame = rect;
    }
    
    - (void)setIfNotEqualTransform:(CGAffineTransform)transform frame:(CGRect)frame
    {
        if(!CGAffineTransformEqualToTransform(self.view.transform, transform))
        {
            self.view.transform = transform;
        }
        if(!CGRectEqualToRect(self.view.frame, frame))
        {
            self.view.frame = frame;
        }
    }
    
    + (CGFloat)getStatusBarHeight
    {
        UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
        if(UIInterfaceOrientationIsLandscape(orientation))
        {
            return [UIApplication sharedApplication].statusBarFrame.size.width;
        }
        else
        {
            return [UIApplication sharedApplication].statusBarFrame.size.height;
        }
    }
    
    static BOOL IS_BELOW_IOS_7()
    {
        static BOOL answer;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            answer = [[[UIDevice currentDevice] systemVersion] floatValue] < 7.0;
        });
        return answer;
    }
    
    + (CGRect)rectInWindowBounds:(CGRect)windowBounds statusBarOrientation:(UIInterfaceOrientation)statusBarOrientation statusBarHeight:(CGFloat)statusBarHeight
    {
        CGRect frame = windowBounds;
    
        if(IS_BELOW_IOS_7())
        {
            frame.origin.x += statusBarOrientation == UIInterfaceOrientationLandscapeLeft ? statusBarHeight : 0;
            frame.origin.y += statusBarOrientation == UIInterfaceOrientationPortrait ? statusBarHeight : 0;
            frame.size.width -= UIInterfaceOrientationIsLandscape(statusBarOrientation) ? statusBarHeight : 0;
            frame.size.height -= UIInterfaceOrientationIsPortrait(statusBarOrientation) ? statusBarHeight : 0;
        }
        return frame;
    }
    
    - (UIInterfaceOrientation)desiredOrientation
    {
        UIInterfaceOrientation statusBarOrientation = [[UIApplication sharedApplication] statusBarOrientation];
        AGInterfaceOrientationMask statusBarOrientationAsMask = [self AGInterfaceOrientationMaskFromOrientation:statusBarOrientation];
        if(self.supportedInterfaceOrientations & statusBarOrientationAsMask)
        {
            return statusBarOrientation;
        }
        else
        {
            if(self.supportedInterfaceOrientations & AGInterfaceOrientationMaskPortrait)
            {
                return UIInterfaceOrientationPortrait;
            }
            else if(self.supportedInterfaceOrientations & AGInterfaceOrientationMaskLandscapeLeft)
            {
                return UIInterfaceOrientationLandscapeLeft;
            }
            else if(self.supportedInterfaceOrientations & AGInterfaceOrientationMaskLandscapeRight)
            {
                return UIInterfaceOrientationLandscapeRight;
            }
            else
            {
                return UIInterfaceOrientationPortraitUpsideDown;
            }
        }
    }
    
    -(CGFloat)UIInterfaceOrientationAngleOfOrientation:(UIInterfaceOrientation)orientation
    {
        CGFloat angle;
    
        switch (orientation)
        {
            case UIInterfaceOrientationPortraitUpsideDown:
                angle = M_PI;
                break;
            case UIInterfaceOrientationLandscapeLeft:
                angle = -M_PI_2;
                break;
            case UIInterfaceOrientationLandscapeRight:
                angle = M_PI_2;
                break;
            default:
                angle = 0.0;
                break;
        }
    
        return angle;
    }
    
    -(AGInterfaceOrientationMask)AGInterfaceOrientationMaskFromOrientation:(UIInterfaceOrientation)orientation
    {
        return 1 << orientation;
    }
    
    //If dealloc is a duplicate method then remove this dealloc method and add the unregisterFromNotifications method in dealloc.
    - (void)dealloc {
        [self unregisterFromNotifications];
    }
    
    -(void)unregisterFromNotifications
    {
        //for orientation
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidChangeStatusBarFrameNotification object:nil];
    }
    
  • Also In subView's .h file add this line.

    - (void)rotateAccordingToStatusBarOrientationAndSupportedOrientations;
    

Now time to use above added code for orientation change in subview.

  • While adding subview in window or self.view like this :

    [objAppDelegate.window addSubview:objViewController.view];
    
    //added this method to rotate subview according to orientation when added
    [objViewController rotateAccordingToStatusBarOrientationAndSupportedOrientations];
    

Reference taken from AGWindowView

瞄了个咪的 2024-12-23 15:35:36

窗口子视图不会自行旋转,它们通常在视图控制器的帮助下旋转。

您可以将要旋转的视图添加到视图控制器,然后将其添加到窗口,或者您可以注册设备方向更改通知并自行旋转它们。

Window subviews don't rotate by themselves, they usually rotate with the help of a view controller.

You could either add the views you want rotated to a view controller and then add that to the window, or you could register for device orientation changes notifications and rotate them yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文