【问题标题】:Adding uiTextView before uiScrollView在 uiScrollView 之前添加 uiTextView
【发布时间】:2013-03-19 07:42:40
【问题描述】:

我希望 textview 像这样在我的 tablecell 内的滚动视图之前,以便滚动视图滚动并且标题保持不变。

[Title goes here] - it does not move with the scrollview
---------------------
|                   |
|    ScrollView     |
|                   |
---------------------

这是我的代码,textView 出现在滚动视图的后面。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MainCell"];
    }

    NSInteger numberOfViews = 10;
    UIScrollView *vwScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 1280, 200)];

    for (int i = 0; i < numberOfViews; i++) {
        CGFloat xOrigin = i * 250;
        UITextView *titleView = [[UITextView alloc]  initWithFrame:CGRectMake(0, 0, 1280, 20)];
        titleView.text = @"Title";
        [titleView setFont:[UIFont fontWithName:@"HelveticaBold" size:32]];


        UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(xOrigin, 0, 250, 180)];
        awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5/i/10 blue:0.5 alpha:1];
        [awesomeView.layer setBorderColor:[UIColor whiteColor].CGColor];
        [awesomeView.layer setBorderWidth:10.5f];
        awesomeView.layer.shadowPath = [UIBezierPath bezierPathWithRect:awesomeView.bounds].CGPath;

        [cell.contentView addSubview:titleView];
        [vwScroll addSubview:awesomeView];

    }
    vwScroll.contentSize = CGSizeMake(250 * numberOfViews, 200);

    [cell.contentView addSubview:vwScroll];

    return cell;
}

【问题讨论】:

    标签: ios uiscrollview uitextfield


    【解决方案1】:

    为什么不能将 titleView 的框架设置为 (0,0,width,titleHeight),然后将 scrollView 的框架设置为 (0,titleHeight,width,cellHeight-titleHeight)

    这只会将标题放在单元格的顶部,将其高度设置为您使 titleHeight 成为的任何值,并将滚动视图放在单元格的底部,将其高度设置为其余部分细胞。

    【讨论】:

    • 你把scrollView的Y坐标设置成和textView的高度一样了吗?
    • 啊,这就是问题所在!伙计,我没想到,谢谢,将其添加到您的答案中,我会给您一个绿色标记:-)
    • 答案在 :) scrollView 框架的第二个坐标是titleHeight
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多