使用严格自动布局环境的 UIScrollView 缩放似乎不起作用。
这尤其令人沮丧,因为 iOS 6 发行说明肯定让我相信它应该在此处写到“纯自动布局方法”http://developer.apple.com/library/ios/#releasenotes/General/RN-iOSSDK-6_0/_index.html
我查看了第 202、228 和 232 次 session 的 WWDC 2012 幻灯片,但没有找到答案。
我在互联网上看到的唯一专门针对此问题的问题是 UIScrollView zooming with Auto Layout , 但它没有提供问题的代码,也没有答案。
这个用户https://stackoverflow.com/users/341994/matt对 UIScrollView 自动布局问题给出了很多很好的回应,甚至链接到 git hub 上的代码,但我在那里找不到任何可以回答这个问题的东西。
我试图将这个问题归结为绝对最低限度以使其清楚。
我使用 Storyboard创建了一个新的单 View 应用程序,并且未在界面构建器中进行任何更改。
我在项目“pic.jpg”中添加了一个大图片文件。
SVFViewController.h
#import <UIKit/UIKit.h>
@interface SVFViewController : UIViewController <UIScrollViewDelegate>
@property (nonatomic) UIImageView *imageViewPointer;
@end
SVFViewController.m
#import "SVFViewController.h"
@interface SVFViewController ()
@end
@implementation SVFViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIScrollView *scrollView = [[UIScrollView alloc] init];
UIImageView *imageView = [[UIImageView alloc] init];
[imageView setImage:[UIImage imageNamed:@"pic.jpg"]];
[self.view addSubview:scrollView];
[scrollView addSubview:imageView];
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
imageView.translatesAutoresizingMaskIntoConstraints = NO;
self.imageViewPointer = imageView;
scrollView.maximumZoomScale = 2;
scrollView.minimumZoomScale = .5;
scrollView.delegate = self;
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(scrollView,imageView);
NSLog(@"Current views dictionary: %@", viewsDictionary);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|" options:0 metrics: 0 views:viewsDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]|" options:0 metrics: 0 views:viewsDictionary]];
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[imageView]|" options:0 metrics: 0 views:viewsDictionary]];
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[imageView]|" options:0 metrics: 0 views:viewsDictionary]];
}
-(UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return self.imageViewPointer;
}
@end
请注意,我特别努力使它与 iOS 6 发行说明中提供的示例代码一样,只做了最少的操作来实现缩放。
那么,问题是什么?
当您运行此应用程序并在 ScrollView 中四处移动时,一切都很好。但是,当您缩放时,问题很明显,图像来回闪烁,每次缩放时图像在 ScrollView 中的位置都会变得更加错误。
看起来 imageView 的内容偏移量正在发生争斗,似乎每次“缩放”时它都被两个不同的东西设置为不同的值。 (imageView 的内容偏移属性的 NSLog 似乎证实了这一点)。
我在这里做错了什么?有谁知道如何在纯自动布局环境中的 UIScrollView 中实现缩放。那里有这样的例子吗?
请帮忙。
最佳答案
再次重读iOS SDK 6.0 release notes我发现:
Note that you can make a subview of the scroll view appear to float (not scroll) over the other scrolling content by creating constraints between the view and a view outside the scroll view’s subtree, such as the scroll view’s superview.
解决方案
将您的 subview 连接到外部 View 。换句话说,到嵌入 ScrollView 的 View 。
并按照以下方式应用约束,我已经成功了:
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|" options:0 metrics: 0 views:viewsDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]|" options:0 metrics: 0 views:viewsDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[imageView(width)]" options:0 metrics:@{@"width":@(self.imageViewPointer.image.size.width)} views:viewsDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[imageView(height)]" options:0 metrics:@{@"height":@(self.imageViewPointer.image.size.height)} views:viewsDictionary]];
关于ios - UIScrollView 缩放不适用于自动布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14433109/