我遇到了这个问题,我在滚动视图中添加了水平滚动视图和UITextview
,但在模拟器中视图无法滚动。
我的问题是:如何设置内容大小以使其滚动?
以下是代码:
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.title = scenery.name;
mainScrollView = [[UIScrollView alloc] init];
self.imagePaths = [scenery imagePaths];
imageScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 240)];
imageScrollView.directionalLockEnabled = YES;
imageScrollView.pagingEnabled = YES;
imageScrollView.showsVerticalScrollIndicator = NO;
imageScrollView.showsHorizontalScrollIndicator = NO;
[self AddImgsToScrollView];
self.currentPage = 0;
[NSTimer scheduledTimerWithTimeInterval:1 target: self selector: @selector(handleTimer:) userInfo:nil repeats: YES];
[mainScrollView addSubview:imageScrollView];
//Introduction Text View
introView = [[UITextView alloc] init];
introView.text = scenery.introduction;
introView.editable = NO;
introView.font = [UIFont systemFontOfSize:16.0f];
[introView setScrollEnabled:NO];
CGSize textViewSize = [introView.text sizeWithFont:introView.font constrainedToSize:CGSizeMake(WIDTH-20, FLT_MAX)];
CGRect newFrame = introView.frame;
newFrame.size = textViewSize;
newFrame.origin = CGPointMake(10,245);
introView.frame = newFrame;
[mainScrollView addSubview:introView];
CGFloat scrollViewHeight = 0.0f;
for (UIView *view in mainScrollView.subviews){
if (scrollViewHeight < view.frame.origin.y + view.frame.size.height) scrollViewHeight = view.frame.origin.y + view.frame.size.height;
}
newFrame = mainScrollView.frame;
newFrame.size = CGSizeMake(WIDTH, scrollViewHeight);
mainScrollView.frame = newFrame;
NSLog(@"%f/%f", scrollViewHeight,textViewSize.height);
[mainScrollView setContentSize:CGSizeMake(WIDTH, scrollViewHeight)];
[self.view addSubview:mainScrollView];
}
答案 0 :(得分:0)
尝试以下方法。这是我的工作代码。
int scrollWidth=60;
for (int i=0;i<array.count;i++)
{
UIImageView *imageView1=[[UIImageView alloc]initWithFrame:CGRectMake(scrollWidth,8,50,40)];
imageView1.userInteractionEnabled=YES;
imageView1.backgroundColor=[array objectAtIndex:i];
[scrollView addSubview:imageView1];
scrollWidth=scrollWidth+80;
}
[scrollView setContentSize:CGSizeMake(scrollWidth, 30)];
同时检查xib是否启用用户交互和滚动。
答案 1 :(得分:0)
最后,我得到了答案。
CGRect newFrame = introView.frame;
newFrame.size = textViewSize;
newFrame.origin = CGPointMake(10,245);
introView.frame = newFrame;
[mainScrollView addSubview:introView];
CGFloat scrollViewHeight = introView.contentSize.height + imageScrollView.contentSize.height + 64;
newFrame = mainScrollView.frame;
newFrame.size = CGSizeMake(WIDTH, 480);
newFrame.origin = CGPointMake(0,0);
mainScrollView.frame = newFrame;
[mainScrollView setContentSize:CGSizeMake(WIDTH, scrollViewHeight)];
<强> 1。将mainScrollView框架设置为屏幕高度。
<强> 2。然后使用setContentSize设置所有子视图的高度。