2013-02-28 22 views
1

我想在用户输入无效的电子邮件时显示警告气泡。我可以成功地显示出泡沫,但同时存在气泡,如果方向改变泡沫与的UITextField重叠Uitextfield和UIImageView在方向更改时重叠

查看启动景观: enter image description here

方向变为纵向:

enter image description here

其他各地方式:

查看开始为肖像:

enter image description here

方向变为横向(泡去更远)

enter image description here

我的代码:

//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

+0

您使用的是iOS 6吗?在这种情况下,您可以使用自动布局约束来代替自动调整遮罩,这些遮罩更强大并且允许这种约束。 – 2013-02-28 15:33:19

+0

不,这是5.1。应用程序必须兼容IOS 5.1及以上版本。 – 2013-02-28 15:35:42

回答

0

这是因为autoResizingMask的

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的引用。

+0

你是什么意思'设置origin.x在方向变化将解决它',所以我必须检测方向变化与通知并应用您的解决方案时收到通知? – 2013-02-28 16:11:18

+0

这将是一种方式,或者在视图控制器的'shouldAutorotateToInterfaceOrientation:'中,检查是否存在气泡并在其中写入此代码。但在做所有看到如果更改autoResizeMask解决问题(即使您没有完全控制)之前 – Rakesh 2013-02-28 16:15:42

+0

我已经尝试过'UIViewAutoresizingFlexibleRightMargin',然后发布我的问题它不工作。 – 2013-02-28 16:24:28