objective-c - 显示可滚动项 Objective-C
问题描述
对于我的应用程序,我有一个 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;
}
}
现在这大致可以工作,但我想知道是否有更通用的方法?
谢谢
解决方案
你在正确的轨道上。我们只需要一个公式来描述所需的条件:文本过多,文本超出视图底部
- (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;
}
软糖是因为视图高度可能不是给定字体的行高的整数倍。不止一条线似乎可以解决问题。
推荐阅读
- c# - HTML中的连续图片
- thymeleaf - 如何将 message.properties 值传递给 Thymeleaf 中的 String 实用程序方法
- javascript - 不可能的错误...并发问题?NextJs / 节点 HTTP 服务器
- r - 创建从数据框中调用值的重复文本行/变量
- pyspark - Databricks 笔记本中的多选小部件
- regex - 转换href tel标签中的数字
- c# - 使用 MassTransit SignalR 背板时集线器未收到任何消息
- python - TypeError:不支持的操作数类型/:'str'和'int'---对于X_train.columns中的col:
- javascript - 平局结果无法输出
- latex - 未找到“libertineotf.sty”