方向改变时,Uitextfield和UIImageView重叠

时间:2013-02-28 15:29:15

标签: ios objective-c orientation autoresizingmask

我想在用户输入无效的电子邮件时显示警告气泡。我可以成功地显示气泡,但是如果方向改变气泡与uitextfield重叠,则会出现气泡

以景观视图开始: 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

1 个答案:

答案 0 :(得分: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的引用。