将您的应用程序切换到横屏模式并执行以下代码:
UIWindow *toastWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
toastWindow.hidden = NO;
toastWindow.backgroundColor = [[UIColor cyanColor] colorWithAlphaComponent:0.5f];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[toastWindow removeFromSuperview];
});
在iOS 7中,您会在整个屏幕上获得一个透明的蓝色覆盖层,该覆盖层在5秒后消失。在iOS 8中,您将获得一个透明的蓝色覆盖层,该覆盖层覆盖了一半以上的屏幕。显然,这与Apple在iOS 8中所做的更改有关,其中屏幕坐标现在是面向接口而不是面向设备的,但以Apple的风格,它们似乎在横向模式和旋转中留下了无数的错误。
我可以通过检查设备方向是否为横向并翻转主屏幕边界的宽度和高度来“修复”这个问题,但那似乎是一种可怕的黑客方式,而且当Apple在iOS 9中再次更改一切时,它可能会被打破。
CGRect frame = [[UIScreen mainScreen] bounds];
if (UIInterfaceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
frame.size.width = frame.size.height;
frame.size.height = [[UIScreen mainScreen] bounds].size.width;
}
UIWindow *toastWindow = [[UIWindow alloc] initWithFrame:frame];
toastWindow.hidden = NO;
toastWindow.backgroundColor = [[UIColor cyanColor] colorWithAlphaComponent:0.5f];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[toastWindow removeFromSuperview];
});
有人遇到过这个问题,并找到了更好,不那么脆弱的解决方案吗?
编辑:我知道我可以使用UIView并将其添加到关键窗口,但我想将某些内容放在状态栏上方。
UIWindowLevel
堆叠级别自iOS 2.0以来就已经在UIKit中提供,并且随着iOS 7的出现,使用这个API的原因变得更加充分! - runmad