UIScrollView - 缩放问题(在任何缩放后切断 UIScrollView 内 UIView 的底部)
因此,对我的视图在出现问题之前所做的事情进行一些解释,页面加载了一堆可单击到 contentView 中的按钮,contentView 是我的滚动视图的子视图。
我有一个 UIViewController,里面有我的 UIScrollView 滚动视图来处理滚动和缩放 我将所有 UIButtons 添加到的 UIView contentView 。
好吧,我会尝试只放置我认为可能需要更改的代码。
在 viewDidLoad 中
scrollView.contentSize = CGSizeMake(320, (20+totalRows*OFFSET_Y) );
[scrollView setMaximumZoomScale:4];
[scrollView setMinimumZoomScale:1];
[scrollView setDelegate:self];
[contentView setFrame:CGRectMake(0, 0, 320, (20+totalRows*OFFSET_Y) )];
[self.scrollView addSubview:contentView];
进行一些计算,看看我需要多大的内容大小才能适合所有按钮,然后我将 contentView 作为子视图添加到我的滚动视图中。 然后,我将所有 UIButtons 添加为 contentView 的子视图,
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return [self contentView];
}
获取用于缩放返回 contentView 的委托方法。
好吧,所以一切都是可滚动和可缩放的,就像它应该的那样......我所有的 UIButtons 仍然是可点击的,并且按应有的方式工作。
问题是,当我放大/缩小后,当我一直滚动到滚动视图的底部时,它会从底部被切断,因此最后一行按钮被切成两半。即使我重新缩放到 1:1,它仍然被切断。
我的应用程序中有一个 44 像素的导航控制器,不确定这是否以某种方式搞砸了。
我正在检查scrollView和contentView的contentSize,在完成任何缩放之前,我的scrollView的高度比contentView大44像素,但是在任何缩放之后,比率是1:1,这似乎是问题所在。 不过,我不会更改代码中任何地方的大小。
任何帮助表示赞赏! 谢谢
就像我在添加这段代码之前所想的那样
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale (float)scale
{
self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width, self.scrollView.contentSize.height + (44 * scale));
}
现在缩放不会切断任何东西并且似乎工作正常。
我仍然不相信这是我应该做的,但我想它现在有效。
还是希望有人有更好的答案!
在玩了更多我的应用程序后,我意识到缩放从来都不是问题。实际的问题是我的 UIView *contentView。更改滚动视图和内容视图的背景颜色(更改为两种非常不同的颜色)后,我注意到我的内容视图不够长,无法覆盖所有按钮。
因此,第一次加载时,scrollView足够大,可以看到所有内容,但是一旦缩放并且它占用了contentView的大小,它就没有被正确调整!
综上所述,它终于可以正常工作了!...哈哈,没有回复,但希望有人会发现这很有用!最好的建议是改变背景颜色。
So a little explanations of what my view is doing before the problem, the page loads a bunch of buttons that are clickable into the contentView which is a subView of my scrollView.
i got a UIViewController, inside i have my
UIScrollView scrollView to handle the scrolling and zooming
UIView contentView which i add all the UIButtons to.
Alright i'll try to only put the code which i believe might need to be changed.
in viewDidLoad
scrollView.contentSize = CGSizeMake(320, (20+totalRows*OFFSET_Y) );
[scrollView setMaximumZoomScale:4];
[scrollView setMinimumZoomScale:1];
[scrollView setDelegate:self];
[contentView setFrame:CGRectMake(0, 0, 320, (20+totalRows*OFFSET_Y) )];
[self.scrollView addSubview:contentView];
Doing some calculation to see how big i need to content size to be to fit all the buttons, then i add the contentView as subView to my scrollView.
I then add all the UIButtons as subview of my contentView
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return [self contentView];
}
Got my delegate method for zooming returning the contentView.
Ok so everything is scrollable and zoomable like it should be..all my UIButtons are still clickable and works how it should be.
The problem is after i zoom in/out when i scroll all the way to the bottom of my scrollView it's being cut off from the bottom, so the last row of buttons are being cut in half. even when i rezoom to 1:1 it's still cut off.
I've got a NavigationController in the app which is 44 pixels, not sure if that's screwing something up somehow.
I was checking the contentSize of the scrollView and contentView and before any zooming is done, the Height of my scrollView is 44 pixels bigger than the contentView, but after any zooming the ratio is 1:1 and that seems to be the problem.
I'm not changing the size anywhere in the code though.
Any help is appreciated!
Thanks
Like i thought before I added this piece of code
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale (float)scale
{
self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width, self.scrollView.contentSize.height + (44 * scale));
}
And now Zooming doesn't cut anything off and seems to be working correctly.
I still don't believe this is what I should have to do but I guess it works for now.
Still hoping someone has a better answer!
After playing more with my app I realized that the zooming was never the problem to begin with. The actual problem was with my UIView *contentView. After changing the background colour of my scrollView and my contentView (to 2 very diff colours) I noticed that my contentView wasn't long enough to cover all of the buttons.
So at first load the scrollView was big enough to see everything but once you zoom and it takes the size of the contentView it wasn't being adjusted properly!
All that to say, it finally works how it should!..haha no replies but hopefully someone might find this useful! Best tip was to change the background colours.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
再会,
我意识到游戏很晚了,但想更新我在这方面发现的内容,以防其他人遇到这个问题。
所做的就是将 UIImageView 和 UIScrollView 设置为“Aspect Fit”,这解决了我的问题。这可能是 Xcode 7 的一项功能,但它确实有效。
Good Day,
I realize very late to the game, but wanted to updated what i found on this, should anyone else come across this issue.
All did was set BOTH the UIImageView and the UIScrollView to "Aspect Fit" and this fixed the issue for me. This may be an Xcode 7 feature, but it works.