UIscrollview调整大小问题

时间:2011-06-08 05:31:04

标签: iphone objective-c ios ios4 uiscrollview

我有一个srollview,其中包含文本字段和标签作为其子视图,我想要显示UIpickerview的两个文本字段。 例如:当用户触摸textfield11时,我想显示一个从屏幕底部向上滑动的选择器,此时我想改变我的scrollview的高度,但它不起作用。

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 height = 200.000000 before scrolling

after scrolling

2 个答案:

答案 0 :(得分:1)

这可能会对您有所帮助:

[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文件中将它们全部设为全局,然后您将更好地控制其行为。您的选择器视图与其他滚动视图项一起移动,因为您可能在选择滚动视图时将选择器视图添加为子视图。

答案 1 :(得分:0)

您必须在视图中更改内容大小和内容偏移量和选择器视图而不是滚动视图,因为如果您在滚动视图中添加它,则无法从选择器视图中选择值。