简体   繁体   English

计算uitableview的滚动指示器大小

[英]Calculate scroll indicator size of uitableview

I am displaying the custom scroll bar in my uitableview. 我在uitableview中显示自定义滚动条。 So I want to calculate the size/height of scroll indicator. 所以我想计算滚动指示器的大小/高度。 So please suggest me how should I calculate this. 所以请建议我该如何计算。

thanks 谢谢

Update 更新

My apologies, my formula is incomplete. 抱歉,我的公式不完整。

I got confused between my percentage and frame height. 我对自己的百分比和框架高度感到困惑。

So the proper formula is like this: 所以正确的公式是这样的:

scrollBar percentage height = table view height / table view contentSize

scrollBar height = scrollBar percentage height * table view height

Translating this into code again: 再次将其转换为代码:

CGSize scrollBarSize;

scrollBarSize.width = 10.0;

CGFloat percentageHeight = self.tableView.bounds.size.height / self.tableView.contentSize.height;

scrollBarSize.height = percentageHeight * self.tableView.bounds.size.height;

NSLog(@"scrollBar height = %lf", scrollBarSize.height);

Here's a photoshop screenshot example, try using the rectangle selection marquee to measure the scrollBar height in my screenshot, it should come out about 166.644 px: 这是一个photoshop屏幕截图示例,请尝试使用矩形选择选取框来测量屏幕截图中的scrollBar高度,它应该显示为166.644 px:

scrollBar高度截图

Original Answer 原始答案

I think the scroll indicator height is a visual representation of the visible percentage of entire tableView content size. 我认为滚动指示器的高度是整个tableView内容大小的可见百分比的直观表示。

So maybe you can calculate it by taking the height of the device screen, divide by the tableView's content size height: 因此,也许您可​​以通过将设备屏幕的高度除以tableView的内容大小高度来计算它:

scroll bar height = table view height / table view contentSize

Translating this into code might be something like: 将其转换为代码可能类似于:

CGSize scrollBarSize;

scrollBarSize.width = 10.0;

scrollBarSize.height = self.tableView.bounds.size.height / self.tableView.contentSize.height;

NSLog(@"scrollBar height = %lf", scrollBarSize.height);

Does this work for you? 这对您有用吗?

CGRect scrollFrame = CGRectMake(self.view.frame.size.with - (SCROLLBAR_WIDTH + PADDING),
                                (self.scrollView.contentOffset * self.scrollView.frame.size.height) / self.contentSize.height,
                                SCROLLBAR_WIDHT, 
                                SCROLLBAR_HEIGHT);

Try this by replacing some variables with the ones in your context. 通过用上下文中的变量替换一些变量来尝试此操作。

Set this in - (void)scrollViewDidScroll:(UIScrollView *)scrollView delegate method so the scrollBar stays updated with correct position. - (void)scrollViewDidScroll:(UIScrollView *)scrollView委托方法中进行设置,以便scrollBar保持正确位置的更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM