Ios 方向改变时,UIExtField和UIImageView重叠
我想在用户输入无效电子邮件时显示一个警告气泡。我可以成功地显示气泡,但如果方向发生变化,则气泡会与uitextfield重叠 以横向视图开始: 方向变成纵向: 另一方面: 以纵向视图开始: 方向变成了风景(气泡走得更远) 我的代码:Ios 方向改变时,UIExtField和UIImageView重叠,ios,objective-c,orientation,autoresizingmask,Ios,Objective C,Orientation,Autoresizingmask,我想在用户输入无效电子邮件时显示一个警告气泡。我可以成功地显示气泡,但如果方向发生变化,则气泡会与uitextfield重叠 以横向视图开始: 方向变成纵向: 另一方面: 以纵向视图开始: 方向变成了风景(气泡走得更远) 我的代码: //image for warnings //get screen size CGRect screenRect = [[UIScreen mainScreen] bounds]; float bubleOri
//image for warnings
//get screen size
CGRect screenRect = [[UIScreen mainScreen] bounds];
float bubleOriginx;
//detect device orientation
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if(orientation == UIInterfaceOrientationLandscapeLeft || orientation== UIInterfaceOrientationLandscapeRight)
{
bubleOriginx=screenRect.size.width*0.95;
}else
{
bubleOriginx=screenRect.size.width*0.72;
}
UIImage *bubble = [[UIImage imageNamed:@"create event button.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(15, 21, 15, 21)];
self.emailImage = [[UIImageView alloc] initWithImage:bubble];
self.emailImage.frame = CGRectMake(bubleOriginx, self.email.frame.origin.y+28, 0, 0);
UILabel *xlabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
xlabel.font = [UIFont fontWithName:@"Helvetica" size:15.0];
xlabel.text = string;
xlabel.numberOfLines = 0;
CGSize labelSize = [xlabel.text sizeWithFont:xlabel.font
constrainedToSize:xlabel.frame.size
lineBreakMode:xlabel.lineBreakMode];
xlabel.frame = CGRectMake(
xlabel.frame.origin.x, xlabel.frame.origin.y,
xlabel.frame.size.width, labelSize.height);
[xlabel setBackgroundColor:[UIColor clearColor]];
[self.emailImage addSubview:xlabel];
self.emailImage.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin;
[self.view addSubview:self.emailImage];
[UIView animateWithDuration:0.85
animations:^(void) {
self.emailImage.frame = CGRectMake(bubleOriginx, self.email.frame.origin.y+28, 220, -60);
xlabel.frame = CGRectMake(10, 0, 210, 60);
} completion:^(BOOL finished) {
}];
如何稳定该UIImageview
使其不会重叠?假设它将始终与uitextfield
的端点保持+30点的距离,或者至少不会重叠
谢谢,
Mord这是因为自动重设屏幕
self.emailImage.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin;
保持UIViewAutoresizingFlexibleRightMargin
;(但不会按照你期望的方式行事)
但将origin.x设置为方向更改将解决此问题
CGRect tempFrame = self.emailImage.frame;
tempFrame.origin.x = textField.frame.origin.x+textField.frame.size.width+30;
self.emailImage.frame = tempFrame;
适用于所有方向。假设textField是对图像中显示的UITextField的引用。您使用的是iOS 6吗?在这种情况下,您可以使用自动布局约束,而不是自动调整遮罩的大小,这更强大,并允许这种约束。不,这是5.1。应用程序必须与IOS 5.1及以上版本兼容。设置原点是什么意思?方向更改时的x将解决此问题,因此我必须通过通知检测方向更改,并在收到通知时应用您的解决方案?这将是一种方式,或者在视图控制器的
应自动旋转指针面方向:
,检查是否存在气泡,并在此处编写此代码。但是在做所有这些之前,看看更改autoResizeMask是否解决了问题(即使您没有完全控制权),我已经尝试了uiviewAutoResizingFlexiblerRightMargin
,然后才发布我的问题。好的。然后你可以用另一种方法来解决问题。但是,如果可以为UITextField
设置固定的左边距,那么将相同的autoResizingMask
设置为文本字段和冒泡设置为不带UIViewAutoResizingFlexibleLeftMargin
,应该可以解决问题。