简体   繁体   English

在iOS中的UIScreen中定位UIWindow

[英]Positioning UIWindow in UIScreen in iOS

I need to add additional UIWindow to my App. 我需要向我的应用添加其他UIWindow This UIWindow should always be positioned in the bottom right corner of the screen despite device's orientation, here's the sketch: 无论设备的方向如何,此UIWindow都应始终位于屏幕的右下角,这是草图: 在此处输入图片说明

I've tried to subclass UIWindow like this to be able to set the size and the margins of window: 我试图像这样子类化UIWindow以便能够设置窗口的大小和边距:

@interface MyWindow : UIWindow

@property (nonatomic) CGSize size;
@property (nonatomic) CGFloat margin;

@end

@implementation MyWindow

- (id)initWithSize:(CGSize)size andMargin:(CGFloat)margin {
    self.size = size;
    self.margin = margin;
    return [self initWithFrame:[self calculateFrame]];
}

- (CGRect)calculateFrame {
    return CGRectMake([[UIScreen mainScreen] bounds].size.width-self.size.width-self.margin, [[UIScreen mainScreen] bounds].size.height-self.size.height-self.margin, self.size.width, self.size.height);
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self assignObservers];
    }
    return self;
}

-(void)assignObservers {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(statusBarDidChangeFrame:)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:nil];
}

- (void)statusBarDidChangeFrame:(NSNotification *)notification {
    [self setFrame:[self calculateFrame]];
}

@end

Everything is great on startup! 一切都很好启动! No matter what orientation is on initial start new UIWindow position is correct. 无论最初启动时是什么方向,新的UIWindow位置都是正确的。 But when I rotate device - my window goes crazy, it jumps to unexpected positions and I can't figure out why. 但是,当我旋转设备时-我的窗口发疯了,它跳到了意外的位置,我不知道为什么。

Please help! 请帮忙!

Everything works fine if: 在以下情况下一切正常:

dispatch_async(dispatch_get_main_queue(), ^{
    [self setFrame:[self calculateFrame]];
}); 

So full working code looks like this: 因此完整的工作代码如下所示:

@interface MyWindow : UIWindow

@property (nonatomic) CGSize size;
@property (nonatomic) CGFloat margin;

@end

@implementation MyWindow

- (id)initWithSize:(CGSize)size andMargin:(CGFloat)margin {
    self.size = size;
    self.margin = margin;
    return [self initWithFrame:[self calculateFrame]];
}

- (CGRect)calculateFrame {
    return CGRectMake([[UIScreen mainScreen] bounds].size.width-self.size.width-self.margin, [[UIScreen mainScreen] bounds].size.height-self.size.height-self.margin, self.size.width, self.size.height);
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self assignObservers];
    }
    return self;
}

-(void)assignObservers {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(statusBarDidChangeFrame:)
                                                 name:UIApplicationDidChangeStatusBarOrientationNotification
                                               object:nil];
}

- (void)statusBarDidChangeFrame:(NSNotification *)notification {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self setFrame:[self calculateFrame]];
    });
}

@end

Many thanks to Cocoa-Chat and max.lunin :) 非常感谢Cocoa-Chat和max.lunin :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM