对我们这些人来说,我的问题可能很简单,但我无法找到解决方案。我有一种自动创建视图的方法(文本字段,标签和文本视图)。这就像一张登记表。问题是,当我将屏幕更改为横向模式时,我想更改视图宽度。为此,我使用了[self.view removeFromSuperview]
并再次创建了视图。问题是视图不会使用横向宽度重新创建。我无法使用IB在方向更改时自动调整视图。视图在viewDidLoad
中创建,并在- (void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation {}
中删除并重新创建。我不知道为什么删除后不会重新创建这些视图。如果有其他解决方案可以与我分享,我会很感激。
以下是我创建视图的方式:
-(void)createViews:(int)width
{
for(int i = 0; i < numberOfTextfields; i++) {
textfieldPadding = textfieldPadding+40;//set some space between the text fields
labelPadding = labelPadding+40;//set some space between the labels
UITextField *field = [[UITextField alloc] initWithFrame:
CGRectMake(10,i*textfieldHeight+textfieldPadding+firstTextfieldHeight+10,width, textfieldHeight)];
field.autoresizingMask = UIViewAutoresizingFlexibleWidth;
//field.backgroundColor= [UIColor cyanColor];
field.placeholder = [labels objectAtIndex:i];
field.borderStyle=UITextBorderStyleRoundedRect;
field.returnKeyType = UIReturnKeyDone;
[field addTarget:self action:@selector(doneButton:)
forControlEvents:UIControlEventEditingDidEndOnExit];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, i*labelHeight+firstLabelHeight+labelPadding-20, width, labelHeight)];
label.text = [labels objectAtIndex:i];
//label.backgroundColor = [UIColor brownColor];
[scrollView addSubview:field];
[scrollView addSubview:label];
[textfields addObject:field];
[labels addObject:label];
[field release];
[label release];
}
}
这是我想删除它们并重新创建它们的地方:
- (void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation
{
if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
[self.view removeFromSuperview];
[self createViews:landscapeWidth];
}
else if(orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
[self.view removeFromSuperview];
[self createViews:portraitWidth];
}
}
提前致谢!
答案 0 :(得分:1)
您最初创建视图时,不是仅删除,调整大小,然后重新添加,而是在创建视图时设置相应的autoresizingMask
(link)?
UITextField *myTextField = [[UITextField alloc] initWithFrame:someRect];
// the following will automatically resize the width on orientation change
myTextField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[myView addSubview:myTextField];
[myTextField release];