在 iOS 6 上使用自动布局在 ScrollView 中嵌入 Image

Embed ImageView in ScrollView with Auto Layout on iOS 6(在 iOS 6 上使用自动布局在 ScrollView 中嵌入 ImageView)

本文介绍了在 iOS 6 上使用自动布局在 ScrollView 中嵌入 ImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用具有自动布局的新 iOS 6 SDK 制作非常简单的元素.我有一个 ImageView 并将其嵌入到 ScrollView 中.(一切都使用 Interface Builder 构建)..png 文件已设置,imageView 模式设置为左上角".

I am trying to make very simple element with new iOS 6 SDK with auto layout. I have an ImageView and Embed it in ScrollView. (everything build with Interface Builder). The .png file is set and imageView mode is set to "Top Left".

实施:

#import "ImaginariumViewController.h"

@interface ImaginariumViewController ()
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end

@implementation ImaginariumViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.scrollView.contentSize = self.imageView.image.size;
    self.imageView.frame =
    CGRectMake(0, 0, self.imageView.image.size.width, self.imageView.image.size.height);
}

@end

当我运行应用程序时,图像没有滚动.在关闭自动布局(使用支柱和弹簧)的情况下做同样的事情,我正在滚动.我想问题出在约束上.有人可以帮帮我吗?

When I run the app, the image is not scrolled. Doing all the same with auto layout turned off (with struts and springs), I have working scrolling. I guess the problem is with constraints. Could anybody help me, please?

推荐答案

我刚刚在更新的教程中遇到了同样的问题.我尝试以编程方式删除约束、诅咒并用头撞墙 - 不走运.

I just encountered the same issue in a tutorial that I was updating. I attempted programmatically deleting constraints, cursing, and banging my head against the wall - no luck.

然而,大约 5 分钟前,我尝试了一些解决了我遇到的另一个问题的方法,并且,ta da!UIScrollView 又开始工作了!解决方案是将设置 UIScrollView contentSize 属性的旧代码移动到 viewDidAppear 的实现中,而不是 viewDidLoad:

About 5 minutes ago, however, I tried something that had fixed another issue I encountered, and, ta da! UIScrollView is working again! The solution was to move the old code that sets the UIScrollView contentSize property into an implementation of viewDidAppear, rather than viewDidLoad:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.theScroller.contentSize=CGSizeMake(200.0,2000.0);
}

我希望这可以帮助其他人遇到自动布局出现的一些问题.

I hope this helps someone else encountering some of the headaches that have appeared with Auto Layout.

这篇关于在 iOS 6 上使用自动布局在 ScrollView 中嵌入 ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 iOS 6 上使用自动布局在 ScrollView 中嵌入 Image

基础教程推荐