【问题标题】:Display scrollable item Objective-C显示可滚动项 Objective-C
【发布时间】:2018-10-09 13:52:22
【问题描述】:

对于我的应用程序,我有一个 UITextView,当有可滚动内容时我需要显示一个箭头。

当您位于底部或无法滚动文本时,必须隐藏图像。

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

   self.scrollableItem.hidden = YES;
   float scrollViewHeight = scrollView.frame.size.height;
   float scrollContentSizeHeight = scrollView.contentSize.height;
   float scrollOffset = scrollView.contentOffset.y;

   if (scrollOffset > 0 && scrollOffset <= scrollViewHeight / 2) {
    self.scrollableItem.hidden = NO;
   } else if (scrollOffset <= 0 && scrollContentSizeHeight >= scrollViewHeight) {
    self.scrollableItem.hidden = NO;
   }
}

目前这可以近似地工作,但我想知道是否有更通用的方法?

谢谢

【问题讨论】:

    标签: objective-c scroll uitextview


    【解决方案1】:

    你在正确的轨道上。我们只需要一个公式来描述所需的条件:文本过多,文本超出视图底部

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
        if (scrollView != self.textView) return;
        [self updateScrollableItem:(UITextView *)scrollView];
    }
    
    - (void)textViewDidChange:(UITextView *)textView {
        [self updateScrollableItem:textView];
    }
    
    - (void)updateScrollableItem:(UITextView *)textView {
        CGSize contentSize = textView.contentSize;
        CGSize boundsSize = textView.bounds.size;
        CGFloat contentOffsetY = textView.contentOffset.y;
    
        BOOL excess = contentSize.height > boundsSize.height;
        // notice the little fudge to make sure some portion of a line is above the bottom
        BOOL bottom = contentOffsetY + textView.font.lineHeight * 1.5 > contentSize.height - boundsSize.height;
    
        self.scrollableItem.hidden = !excess || bottom;
    }
    

    这是因为视图高度可能不是给定字体的行高的整数倍。多一点似乎就可以解决问题。

    【讨论】:

    • 像魅力一样工作!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-12
    • 2014-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多