iPhone - 编辑UITextField向右移动其他视图

时间:2015-08-19 23:33:35

标签: ios objective-c iphone uitextfield alignment

在我加载的初始视图控制器中,我的一个子视图是UITextField。我还有主视图的其他子视图(即四个标签,一个选择器视图和两个按钮)。当我尝试编辑文本字段时,所有其他子视图都向右移动。我不知道为什么会这样。我没有代码甚至适用于编辑文本字段时发生的事情。但是,我确实有一些代码用于对齐我的所有子视图(包括文本字段)。这是代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.stopPickerView.dataSource = self;
    self.stopPickerView.delegate = self;
    selectedEntry=@"";
    self.inputField.delegate=self;

    // Do any additional setup after loading the view, typically from a nib.



}

-(void)viewDidAppear:(BOOL)animated{

    int width=self.view.bounds.size.width;
    int height=self.view.bounds.size.height;

    headingLabel.frame=CGRectMake(headingLabel.frame.origin.x, headingLabel.frame.origin.y,self.view.bounds.size.width-self.view.bounds.size.width/5, headingLabel.frame.size.height);
    headingLabel.center=CGPointMake(width/2,height/9);


    stopPickerView.center=CGPointMake(width/2, height/5);
    setStop.center=CGPointMake(width/2, height/2.2);
    orLabel.center=CGPointMake(width/2, height/1.7);
    wakeUpLabelOne.frame=CGRectMake(wakeUpLabelOne.frame.origin.x, wakeUpLabelOne.frame.origin.y,self.view.bounds.size.width-self.view.bounds.size.width/2, self.view.bounds.size.height-self.view.bounds.size.height/19.2);


          wakeUpLabelOne.center=CGPointMake(width/3.5, height/1.4);
    NSLog(@"%f",wakeUpLabelOne.frame.size.width);
    wakeUpLabelTwo.center=CGPointMake(width/1.1, height/1.4);
    inputField.center=CGPointMake(width/1.5, height/1.4);
    finalSetButton.center=CGPointMake(width/2, height/1.2);
    [wakeUpLabelTwo adjustsFontSizeToFitWidth];


}

- (int)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

    selectedEntry = [_pickerData objectAtIndex:row];

}

// The number of rows of data
- (int)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _pickerData.count;
}

// The data to return for the row and component (column) that's being passed in
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return _pickerData[row];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [inputField resignFirstResponder];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return NO;
}

如果您对此有何看法或想了解更多信息,请将其留在评论中。

1 个答案:

答案 0 :(得分:0)

我弄清楚为什么会这样。出于某种原因,将我的框架配置放在viewDidAppear方法中是一个坏主意,并且搞砸了。解决这个问题的方法是将我的框架配置放在viewDidLayoutSubviews方法中。问题立即得到解决。