我有一个视图从屏幕底部弹出,并已加载到我的应用程序委托中。我遇到的问题是我无法设置视图的y坐标,因此它在所有屏幕尺寸上都将显示在相同位置。就我而言,我试图使该弹出视图显示在屏幕底部选项卡栏的上方。
这是我的代码,我在其中放置相对于[[UIScreen mainScreen] bounds]的视图,但是在所有屏幕尺寸上都不一致。如何获取所需的坐标,以便可以在所有屏幕上垂直放置此视图?
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
float y = [UIScreen mainScreen].bounds.size.height;
UIInterfaceOrientation deviceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if (UIInterfaceOrientationIsLandscape(deviceOrientation))
{
[UIView animateWithDuration:0.5
delay:.25
options: UIViewAnimationOptionCurveEaseOut
animations:^{
self->popcontroller.frame = CGRectMake(0,y -90, screenWidth, 40);
}
completion:^(BOOL finished){
}];
}
else{
[UIView animateWithDuration:0.5
delay:.25
options: UIViewAnimationOptionCurveEaseOut
animations:^{
self->popcontroller.frame = CGRectMake(0,y -120, screenWidth, 40);
}
completion:^(BOOL finished){
}];
}
brsAppDelegate *appDelegate = (brsAppDelegate *)[[UIApplication sharedApplication] delegate];
if (![[appDelegate window].subviews containsObject:popcontroller])
{ [[appDelegate window] addSubview:popcontroller];
[popcontroller setNeedsDisplay];
}
});
}
答案 0 :(得分:0)
因此,为了检测边缘插图的额外间距,所有视图上都有一个名为safeAreaInsets
的属性。可以用来检测是否需要为布局参数(顶部/底部或左侧/右侧)添加额外的值。
因此,为了获得“额外利润”,如果应用为纵向,则可以检查该插图的顶部/底部值;如果应用为横向,则可以检查该插图的“顶部/底部”值。
一件要小心的事,此safeAreaInset
默认是在控制器的根视图上设置的,因此,如果您添加了一个自定义视图作为子视图,那么该自定义视图很可能不会设置此属性正确。
更具体地说,在您的情况下,“框架”代码看起来像
if deviceIsInPortrait {
let frame = CGRect(x:thisX,
y: -90 - self.view.safeAreaInsets.top // this will be 0 for non "notch" phones
width: myWidth
height: myHeight
}
else {
let frame = CGRect(x:thisX,
y: -90 - self.view.safeAreaInsets.left // this will be 0 for non "notch" phones
width: myWidth
height: myHeight
}
还有另外一条建议,请尽可能多地使用自动布局。