gpt4 book ai didi

ios - 以编程方式相对于安全区域的底部定位 View

转载 作者:行者123 更新时间:2023-12-01 16:14:06 25 4
gpt4 key购买 nike

我有一个从屏幕底部弹出的 View ,并已加载到我的应用程序委托(delegate)中。我遇到的问题是我无法设置 View 的 y 坐标,因此它会出现在所有屏幕尺寸的相同位置。就我而言,我试图让这个弹出 View 出现在屏幕底部的标签栏上方。

这是我的代码,我将 View 相对于 [[UIScreen mainScreen] bounds] 放置,但它在所有屏幕尺寸上并不一致。我怎样才能获得我需要的坐标,以便我可以将此 View 垂直放置在所有屏幕上的同一位置?

    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];
}
});

}

最佳答案

因此,为了检测边缘插入的额外间距,所有 View 都有一个名为 safeAreaInsets 的属性。 .这可用于检测是否需要为布局参数(顶部/底部或左侧/右侧)添加额外的值。

因此,为了获得您的“额外边距”,如果应用程序是纵向的,您可以检查该插图的顶部/底部值,或者如果应用程序是横向的,您可以检查插图的“左/右”值。

需要注意的一件事,这个 safeAreaInset默认情况下在 Controller 的 Root View 上设置,因此如果您将自定义 View 添加为 subview ,则该自定义 View 很可能不会正确设置此属性。

更具体地说,在您的情况下,“框架”代码看起来像

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
}

还有一条建议,尽可能多地使用自动布局。

关于ios - 以编程方式相对于安全区域的底部定位 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52582281/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com