视图未设置回原始位置

时间:2013-12-04 09:55:07

标签: ios iphone ios7 cgpoint

在我的xib中,我设置了一个文本框...当用户在其中编辑时,我移动该视图很少...但是当我将其返回到其原始位置时,它没有达到其原始位置它仍然很少原来的位置。 这是viewDidLoad

  - (void)viewDidLoad
     {
    [super viewDidLoad];
    if ([self respondsToSelector:@selector(edgesForExtendedLayout)])self.edgesForExtendedLayout = UIRectEdgeNone;
    search=FALSE;
    UIButton *btnOther =  [UIButton buttonWithType:UIButtonTypeSystem]; //UIButtonTypeCustom for image button
    [btnOther setTitle:@"Save" forState:UIControlStateNormal];
    btnOther.titleLabel.font = [UIFont fontWithName:GZFont size:12.0f];
    //    [btnSave setImage:[UIImage imageNamed:@"save.png"] forState:UIControlStateNormal];
    [btnOther addTarget:self action:@selector(btnSaveAction:) forControlEvents:UIControlEventTouchUpInside];
    [btnOther setFrame:CGRectMake(0, 0, 55, 29)];
    dataArray=[[NSMutableArray alloc]init];
    nameSearchArray=[[NSMutableArray alloc]init];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btnOther];
    txtCharityName.hidden=YES;
    txtCharityMail.hidden=YES;
    originalCenter=self.view.center;
    }

这是textfield委托方法。:

 - (void)textFieldDidBeginEditing:(UITextField *)textField {

    if(textField==txtGratuity){
        self.view.center=CGPointMake(originalCenter.x, originalCenter.y-30);
    }
 }

 - (void)textFieldDidEndEditing:(UITextField *)textField {
    self.view.center=CGPointMake(originalCenter.x, originalCenter.y);
    charityId=@"";
    NSLog(@"charityID%@",charityId);
}

这是截图。:

enter image description here enter image description here

2 个答案:

答案 0 :(得分:1)

as per this equation

frame.origin = center - (bounds.size / 2.0)

center = frame.origin + (bounds.size / 2.0)

你应该为文本字段添加30个点以获得正确的位置。

- (void)textFieldDidEndEditing:(UITextField *)textField {

    self.view.center=CGPointMake(originalCenter.x, originalCenter.y+30);
    charityId=@"";
    NSLog(@"charityID%@",charityId);

}

编辑:

在你的

中试试这个
 - (void)textFieldDidBeginEditing:(UITextField *)textField {

           [UIView animateWithDuration:0.25
                          delay:0.0
                        options: UIViewAnimationOptionCurveEaseInOut
                     animations:^{

                       CGRect frame;

                     // let's move our textField 
                        frame = textField.frame;
                        frame.origin.y = frame.origin.y-30;
                        textField.frame=frame;

                     }
                     completion:^(BOOL finished){
                         if(finished)  NSLog(@"Finished !!!!!);
                     }];


     }

并在编辑后将文本字段移动

- (void)textFieldDidEndEditing:(UITextField *)textField {

           [UIView animateWithDuration:0.25
                          delay:0.0
                        options: UIViewAnimationOptionCurveEaseInOut
                     animations:^{

                       CGRect frame;

                     // let's move our textField
                        frame = textField.frame;
                        frame.origin.y = frame.origin.y+30;
                        textField.frame=frame;

                     }
                     completion:^(BOOL finished){
                         if(finished)  NSLog(@"Finished !!!!!);
                     }];


     }

答案 1 :(得分:0)

使用viewWillAppear而不是viewDidLoad方法设置文本字段框架。