使用视图 iOS 滚动 uilabel 时保持标签文本与屏幕居中对齐?

keep label text aligned center to screen while scrolling uilabel with view iOS?

我有一个 UITableView 和 10 个部分。 对于每个部分,我都有一个自定义 header.

在自定义中 header 我有一个标签作为文本值日期的部分标题。

Table 内容大小为 (450,1000)

这里我的标签文本也在滚动,但我希望我的标签文本应该与屏幕居中对齐并且它不应该水平滚动 table。

提前致谢。

仅当 table 的 UITableViewStyle 属性 设置为 UITableViewStylePlain 时,这种行为才常见。如果将其设置为 UITableViewStyleGrouped,headers 将与单元格一起向上滚动。

以下链接可能会帮助您解决问题: 1. UITableView with fixed section headers 2. https://corecocoa.wordpress.com/2011/09/17/how-to-disable-floating-header-in-uitableview/

在敲了我几个小时的脑袋之后,我终于找到了这个解决方案。

只是我用了

(void)scrollViewDidScroll:(UIScrollView *)scrollView*

方法和更改了我的标签框架 with table 查看内容偏移量 .

myLabel.frame = CGRectMake(tableview.contentoffset.x, 0, deviceHeight, 27);

它将使我的标签保持在与屏幕相同的位置,从逻辑上讲它正在改变但看起来像它的 stable 在相同的位置。

    - (void)addButtonsOnScrollViewHorizontal:(int)postion :(UIScrollView*)scrollView
    {
        [scrollView setShowsVerticalScrollIndicator:NO];
        float x = 30;
        for (int i = 1; i <=postion; i++)
        {
            UIButton *button= [UIButton buttonWithType:UIButtonTypeCustom];
            [button setTag:i];
            [button setTitle:[NSString stringWithFormat:@"%i",i] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor colorWithRed:26.0f/255.0f green:188.0f/255.0f blue:156.0f/255.0f alpha:100.0f] forState:UIControlStateSelected];
            button.titleLabel.font =[UIFont fontWithName:@"HelveticaNeue-Bold" size:18.0f];
            CGSize maxSize = CGSizeMake(button.frame.size.width, FLT_MAX);
            CGRect labRect = [button.titleLabel.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:button.titleLabel.font} context:nil];
            [button setFrame:CGRectMake(x, 0, labRect.size.width+10, 30)];
            [button addTarget:self action:@selector(scrollViewDaysbtnClicked:) forControlEvents:UIControlEventTouchUpInside];
            if (i==postion)
            {
                [_scrollViewDays setContentOffset:CGPointMake (button.frame.origin.x+button.frame.size.width/2-70, 0) animated:NO];
                button.titleLabel.font =[UIFont fontWithName:@"HelveticaNeue-Bold" size:24.0f];
                [button setSelected:YES];
                currentSelectionDays =i;
            }
            [scrollView addSubview:button];
            x +=labRect.size.width+20;
        }
        scrollView .contentSize = CGSizeMake(x, 0);
    }


-(void)scrollViewDaysbtnClicked:(UIButton*)sender
{

    for (UIButton *button in _scrollViewDays.subviews)
    {
        if ([button isKindOfClass:[UIButton class]])
        {
            if (button.tag == sender.tag)
            {
                [_scrollViewDays setContentOffset:CGPointMake (button.frame.origin.x+button.frame.size.width/2-70, 0) animated:NO];
                button.titleLabel.font =[UIFont fontWithName:@"HelveticaNeue-Bold" size:24.0f];
                [button setSelected:YES];
                currentSelectionDays =(int)sender.tag;
            }else
            {
                [button setSelected:NO];
                button.titleLabel.font =[UIFont fontWithName:@"HelveticaNeue-Bold" size:18.0f];
            }
        }
    }
}

使用它....