我有一个带文本字段和标签作为其子视图的滚动视图,对于两个文本字段,我想显示UIpickerview。
例如:当用户触摸textfield11时,我想显示一个从屏幕底部向上滑动的选择器,这时我也想更改滚动视图的高度,但是它不起作用。

CGRect scrollframe = scrollView.frame;
    NSLog(@"scrollframe.height=%f, pick height=%f",scrollframe.size.height, pick.frame.size.height);
    scrollframe.size.height -= pick.frame.size.height;
    [UIView beginAnimations:@"start" context:NULL];
    [UIView setAnimationDuration:0.2];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [scrollView setFrame:scrollframe];
    NSLog(@"scroll height = %f",scrollframe.size.height);
    NSLog(@"scrollview height = %f", scrollView.frame.size.height);
    [pick setFrame:CGRectOffset([pick frame], 0, -220)];
    [UIView commitAnimations];


这是控制台日志。

2011-06-08 10:43:31.316 AESDirect [281:207] scrollframe.height = 416.000000,拾取高度= 216.000000
2011-06-08 10:43:31.316 AESDirect [281:207]滚动高度= 200.000000
2011-06-08 10:43:31.317 AESDirect [281:207] scrollview高度= 200.000000

最佳答案

这可以帮助您:

[scrollframe setContentOffset:CGPointMake(scrollframe.contentOffset.x, scrollframe.contentOffset.y-100)];


您可以从调用pickerView的位置通过相同的函数调用上述代码。因此,您不必更改滚动视图的高度。

您的代码应如下所示:

       UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[contentView setBackgroundColor:[UIColor clearColor]];
self.view = contentView;

UIScrollView *horizontalScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[horizontalScroll setBackgroundColor:[UIColor blackColor]];
[horizontalScroll setDelegate:self];
[contentView addSubview:horizontalScroll];

UIPickerView *tempPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 220, 320, 100)];
[tempPicker setHidden:YES];
[contentView addSubview:tempPicker];


在.h文件中将它们全部设置为Global,然后您将可以更好地控制它们的行为。您的选择器视图与滚动视图的其他项一起移动,因为您可能已将选择器视图添加为滚动视图的子视图。

10-08 07:49