【问题标题】:How to stick UIView to top of UIWindow on iOS 7如何在 iOS 7 上将 UIView 粘贴到 UIWindow 的顶部
【发布时间】:2015-02-13 16:10:29
【问题描述】:

我有一个自定义的UIView,即使界面方向发生变化,我也想将它贴在UIWindow 的顶部。这是我的纵向视图

问题在于,先前的 iOS 8 UIWindow 坐标系不会随着方向的变化而改变。所以我需要手动进行所有计算。 首先是改变UIView的变换,我就是用这个方法做的

 -(CGFloat)angleForOrientation:(UIInterfaceOrientation)orientation
{
    CGFloat angle;
    switch (orientation) {
        case UIInterfaceOrientationLandscapeLeft:
            angle = -M_PI /2.0;
            NSLog(@"UIInterfaceOrientationLandscapeLeft");
            break;

        case UIInterfaceOrientationLandscapeRight:
            angle = M_PI /2.0;
            NSLog(@"UIInterfaceOrientationLandscapeRight");
            break;

        case UIInterfaceOrientationPortraitUpsideDown:
            angle = M_PI;
            NSLog(@"UIInterfaceOrientationPortraitUpsideDown");
            break;

        default:
            angle = 0;
            NSLog(@"UIInterfaceOrientationPortrait");
            break;

    }
    return  angle;
}

第二件事是以某种方式将实际坐标系映射到 UIWindow 坐标系,它保持不变。 那么我应该如何计算自定义UIView 的框架,即使用户将视图旋转到其他方向,我也会将相同大小的 UIView 粘贴到屏幕的顶部中心? 例如,这就是视图在横向中的样子

顺便说一下,这张图片是从 iOS 8 版本生成的。为此我做了以下

   self.frame =  CGRectMake(window.bounds.size/2-50, 0, 100, 100);
   CGFloat angle = [self angleForOrientation:orientation];
   self.transform = CGAffineTransformMakeRotation(angle);

我需要做一些类似于 iOS 7 的事情。我怎样才能做到这一点?

感谢您的帮助!

【问题讨论】:

  • 看看这个项目及其代码github.com/hfossli/AGWindowView
  • 谢谢安德里亚。这是我尝试过的第一件事,但它并没有解决我的问题。可能我需要对源代码做一些修改。

标签: ios ios7 uiview ios8 uiwindow


【解决方案1】:

所以最后我想出了如何实现这一点。

我创建了以下方法来计算将根据给定边界将视图粘贴到顶部的矩形。

-(CGRect)getTopRectForBounds:(CGRect)bounds orientation:(UIInterfaceOrientation)orientation window:(UIWindow *)window
{
    CGRect newRect;
    CGFloat statusBarHeight = [self getStatusBarHeight];
    CGSize screenSize = window.screen.bounds.size;
    CGFloat sW = screenSize.width;
    CGFloat sH = screenSize.height;
    CGFloat W = rect.size.width;
    CGFloat H = rect.size.height;
    switch (orientation) {
        case UIInterfaceOrientationLandscapeLeft:
            newRect = CGRectMake(statusBarHeight, (sH-W)/2, H,W);
            break;
        case UIInterfaceOrientationLandscapeRight:
            newRect =  CGRectMake(sW-H-statusBarHeight, (sH-W)/2, H,W);
            break;
        case UIInterfaceOrientationPortraitUpsideDown:
            newRect =  CGRectMake((sW-W)/2, sH-H-statusBarHeight, W,H);
            break;
        default:
            newRect =  CGRectMake((sW-W)/2, statusBarHeight, W,H);
            break;
    }
    return newRect;
}

然后,只要方向改变,我就改变框架。 所以首先我听一下方向的变化

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarFrameOrOrientationChanged:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarFrameOrOrientationChanged:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];

然后在方向更改事件处理程序中更改变换和框架。 (请参阅我的问题以查看处理转换的方法)

CGRect bounds = CGRectMake(0, 0, 100, 100);
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
self.frame =  [self getTopRectForBounds:bounds orientation:orientation window:self.window]
CGFloat angle = [self angleForOrientation:orientation];
self.transform = CGAffineTransformMakeRotation(angle);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-29
    • 1970-01-01
    • 2013-04-13
    • 2020-12-13
    相关资源
    最近更新 更多