gpt4 book ai didi

ios - 更改方向时 UIView 框架未正确更新

转载 作者:行者123 更新时间:2023-12-01 18:23:05 25 4
gpt4 key购买 nike

我定制了UIView带有两个文本标签和两个按钮。它在创建时功能齐全,但现在我试图在屏幕旋转时移动它(即从纵向到横向)。

即使 UI 元素似乎被正确移动,实际的 UIView的框架最终会做一些奇怪的事情,比如变成一个矩形,当它最初是一个正方形并被设置为一个正方形但具有不同的 x、y 位置时。

我如何看到这是通过设置我的 UIView的背景颜色为黑色,并观察到我的背景图像正在绘制的地方,我希望 View 被绘制,但 UIView的背景变成了不同的形状,并且被绘制在了错误的位置。

这是我的代码:

- (void) moveControls
{
CGRect frame;

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
switch (orientation)
{
case UIDeviceOrientationPortrait:
case UIDeviceOrientationPortraitUpsideDown:
frame = CGRectMake(0, 120, 320, 320);
break;
case UIDeviceOrientationLandscapeLeft:
case UIDeviceOrientationLandscapeRight:
frame = CGRectMake(120, 0, 320, 320);
break;
case UIDeviceOrientationFaceDown:
case UIDeviceOrientationFaceUp:
case UIDeviceOrientationUnknown:
return;
}

[self setFrame:frame];

backgroundImage.frame = CGRectMake(0,0, frame.size.width, frame.size.height);
titleLabel.frame = CGRectMake(0,50,frame.size.width,frame.size.height/2);
titleLabel.numberOfLines = 0;
titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
textLabel.frame = CGRectMake(0,frame.size.height/3,frame.size.width,frame.size.height/3);
doneButton.frame = CGRectMake(frame.size.width - 154, frame.size.height - 90, 100, 34);
tellButton.frame =CGRectMake(54, frame.size.height - 90, 100, 34);
}

最佳答案

并没有真正回答所有问题,但解决了问题:
ios: UIView's width and height after changing orientations is inaccurate?

view.center = view.superview.center;
view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;

关于ios - 更改方向时 UIView 框架未正确更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16109327/

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