添加到 UIWindow 的 UIView 中的方向

发布于 2024-08-27 03:04:56 字数 267 浏览 15 评论 0原文

我有一个 UIView,它应该覆盖整个设备(UIWindow)以支持我正在使用核心动画执行的图像放大/缩小效果,其中用户点击 UITableViewCell 上的按钮,然后我缩放关联的图像。

缩放执行完美,我无法弄清楚为什么即使设备处于横向状态,子视图仍处于纵向模式。如下图所示:

在此处输入图像描述

我确实有一个导航控制器,但此视图已直接添加到 UIWindow 中。

I have a UIView which is supposed to cover the whole device (UIWindow) to support an image zoom in/out effect I'm doing using core animation where a user taps a button on a UITableViewCell and I zoom the associated image.

The zooming is performing flawlessly, what I haven't been able to figure out is why the subview is still in portrait mode even though the device is in landscape. An illustration below:

enter image description here

I do have a navigation controller but this view has been added to the UIWindow directly.

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

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

发布评论

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

评论(6

泪意 2024-09-03 03:04:56

您可以在此处了解一些可能的原因:
技术问答 QA1688 - 为什么我的 UIViewController 不随设备旋转?

在您的情况下,您可能会将视图作为另一个子视图添加到窗口中。只有第一个子视图获取旋转事件。您可以做的是将其添加为第一个窗口子视图的子视图。

UIWindow* window = [UIApplication sharedApplication].keyWindow;
if (!window) 
    window = [[UIApplication sharedApplication].windows objectAtIndex:0];
[[[window subviews] objectAtIndex:0] addSubview:myView];    

You can read about some of the possible causes here:
Technical Q&A QA1688 - Why won't my UIViewController rotate with the device?

In your situation its probably the fact that you are adding the view as another subview to the window. Only the first subview gets the rotation events. What you can do is add it as a subview of the first window subview.

UIWindow* window = [UIApplication sharedApplication].keyWindow;
if (!window) 
    window = [[UIApplication sharedApplication].windows objectAtIndex:0];
[[[window subviews] objectAtIndex:0] addSubview:myView];    
笑着哭最痛 2024-09-03 03:04:56

问题

从 iOS 6 开始,只有最顶层的视图控制器(以及
UIApplication对象)参与决定是否轮换
响应设备方向的变化。

https://developer.apple.com/library/content/qa/ qa1688/_index.html

该解决方案

我开源了一个名为 AGWindowView 的 pod。
它会自动处理任何旋转和框架更改,因此您不必担心这一点。

该代码

支持SDK和iOS系统版本的任意组合。相关代码可以在这里找到:
https://github.com/hfossli/AGWindowView/blob/master/来源/AGWindowView.m

The problem

Beginning with iOS 6, only the topmost view controller (alongside the
UIApplication object) participates in deciding whether to rotate in
response to a change of the device's orientation.

https://developer.apple.com/library/content/qa/qa1688/_index.html

The solution

I have open sourced a pod named AGWindowView.
It will automatically deal with any rotation and framechanges so you won't have to worry about that.

The code

It supports any combination of SDK's and iOS system versions. The relevant code can be found here:
https://github.com/hfossli/AGWindowView/blob/master/Source/AGWindowView.m

如歌彻婉言 2024-09-03 03:04:56

我在 UIApplication 上创建了一个类别,它具有用于获取 keyWindow 的第一个子视图的辅助属性和方法。无论如何,这就是您想要覆盖的视图。现在,当您将由 UIViewController 管理的视图添加到该视图时,将调用 shouldRotateToInterfaceOrientation: 方法。

UIApplication+WindowOverlay.h

#import <UIKit/UIKit.h>

@interface UIApplication(WindowOverlay)

    @property (nonatomic, readonly) UIView *baseWindowView;

    -(void)addWindowOverlay:(UIView *)view;

@end

UIApplication+WindowOverlay.m

#import "UIApplication+WindowOverlay.h"

@implementation UIApplication(WindowOverlay)

    -(UIView *)baseWindowView{
        if (self.keyWindow.subviews.count > 0){
            return [self.keyWindow.subviews objectAtIndex:0];
        }
        return nil;
    }

    -(void)addWindowOverlay:(UIView *)view{
        [self.baseWindowView addSubview:view];
    }
@end

以下是如何使用它。

//at the top of the file...or in {yourproject}.pch
#import "UIApplication+WindowOverlay.h

//in a method:

UIView *view = [UIView new];

UIView *window = [UIApplication sharedApplication].baseWindowView;

view.frame = window.bounds;

[window addSubview:view];

//or

[[UIApplication sharedApplication] addWindowOverlay:view];

I created a category on UIApplication that has a helper property and method for getting the first subview of the keyWindow. This is the view you want to overlay anyway. Now when you add a view that is managed by a UIViewController to that view, the shouldRotateToInterfaceOrientation: method is called.

UIApplication+WindowOverlay.h

#import <UIKit/UIKit.h>

@interface UIApplication(WindowOverlay)

    @property (nonatomic, readonly) UIView *baseWindowView;

    -(void)addWindowOverlay:(UIView *)view;

@end

UIApplication+WindowOverlay.m

#import "UIApplication+WindowOverlay.h"

@implementation UIApplication(WindowOverlay)

    -(UIView *)baseWindowView{
        if (self.keyWindow.subviews.count > 0){
            return [self.keyWindow.subviews objectAtIndex:0];
        }
        return nil;
    }

    -(void)addWindowOverlay:(UIView *)view{
        [self.baseWindowView addSubview:view];
    }
@end

and here is how you would use it.

//at the top of the file...or in {yourproject}.pch
#import "UIApplication+WindowOverlay.h

//in a method:

UIView *view = [UIView new];

UIView *window = [UIApplication sharedApplication].baseWindowView;

view.frame = window.bounds;

[window addSubview:view];

//or

[[UIApplication sharedApplication] addWindowOverlay:view];
携余温的黄昏 2024-09-03 03:04:56

这是因为正如您所提到的,您的视图已直接添加到 UIWindow 中,因此当为导航控制器调用旋转方法时,uiview 不会发生任何变化。如果 UIView 是视图控制器视图的子视图,它将旋转。如果由于某种原因无法做到这一点。然后你可以重写这个方法:

// This method is called every time the device changes orientation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
}

每次你的方向改变时也会改变你的视图方向。

This is because as you mention your view has been added directly to the UIWindow, therefore when the method to rotate is called for the navigation controller nothing happens to the uiview. The UIView would rotate if it was a subview of the view controller view. If for some reason this cannot be done. Then you could override this method:

// This method is called every time the device changes orientation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
}

And every time your orientation changes also change your view orientation.

蹲墙角沉默 2024-09-03 03:04:56

我在将视图直接添加到窗口时遇到了类似的问题。也许这会有所帮助:添加到窗口后自动调整 UIView 的大小

I had a similar problem with views being added directly to a window. Maybe this will help: Automatically Sizing UIView after Adding to Window

不如归去 2024-09-03 03:04:56

我如何解决这个问题的另一个解决方案。

定义当前方向:

@interface AJImageCollectionViewController (){
    UIInterfaceOrientation _currentOrientation;
}
@end

然后检查 viewWillLayoutSubviews 中的方向:

- (void)viewWillLayoutSubviews {
    [self checkIfOrientationChanged];
}

- (void)checkIfOrientationChanged {
    UIInterfaceOrientation newOrientation = [[UIApplication sharedApplication] statusBarOrientation];
    BOOL newOrientationIsPortrait = UIInterfaceOrientationIsPortrait(newOrientation);
    BOOL oldOrientationIsPortrait = UIInterfaceOrientationIsPortrait(_currentOrientation);

    // Check if the orientation is the same as the current
    if(newOrientationIsPortrait != oldOrientationIsPortrait){
        _currentOrientation = newOrientation;
        // Do some stuff with the new orientation
    }
}

Another solution how I solved this problem.

Define the current Orientation:

@interface AJImageCollectionViewController (){
    UIInterfaceOrientation _currentOrientation;
}
@end

Then check the orientation in the viewWillLayoutSubviews:

- (void)viewWillLayoutSubviews {
    [self checkIfOrientationChanged];
}

- (void)checkIfOrientationChanged {
    UIInterfaceOrientation newOrientation = [[UIApplication sharedApplication] statusBarOrientation];
    BOOL newOrientationIsPortrait = UIInterfaceOrientationIsPortrait(newOrientation);
    BOOL oldOrientationIsPortrait = UIInterfaceOrientationIsPortrait(_currentOrientation);

    // Check if the orientation is the same as the current
    if(newOrientationIsPortrait != oldOrientationIsPortrait){
        _currentOrientation = newOrientation;
        // Do some stuff with the new orientation
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文