2014-10-09 157 views
4

所以搞清楚滚动视图如何工作后,我用下面的代码来实现它:的UIScrollView滚动

self.scrollView.delegate = self; 
self.scrollView.userInteractionEnabled = YES; 
CGRect view = CGRectMake(0, 0, 320, 750); 
self.scrollView.contentSize = view.size; 

上面的代码按预期工作在Xcode中所有的仿真6.但是,当我运行我的手机(ios7上的iphone4s)时,滚动根本不起作用。自新版本发布以来,人们遇到同样的问题吗?或者我错过了从文档中学到的东西?

+0

您使用的是AutoLayout吗? – Vishal16 2014-10-09 09:23:49

+0

是的我正在使用AutoLayout,但不是大小类。 – 2014-10-09 09:34:16

+0

面对同样的问题。你在iOS7上运行了吗?似乎没有工作。 – Anil 2014-10-14 06:29:58

回答

-2

我没有尝试Vishu的答案,但我所做的就是更新到iOS 8所以它的兼容Xcode 6和它的工作!

0

在自动版式

一般而言,自动布局考虑顶部,左边,底部,和一个视图的右边缘是可见的边缘。也就是说,如果您将视图固定在其超级视图的左侧边缘,则确实将其视为超级视图边界的最小x值。更改超级视图的边界原点不会改变视图的位置。

UIScrollView类通过更改其边界的原点来滚动其内容。为了使此功能适用于自动布局,滚动视图中的顶部,左侧,底部和右侧边缘现在意味着其内容视图的边缘。

对滚动视图的子视图的约束必须导致要填充的大小,然后将其解释为滚动视图的内容大小。 (这不应与用于自动布局的intrinsicContentSize方法混淆)。要使用自动布局调整滚动视图的框架,约束必须是关于滚动视图的宽度和高度的显式,或者滚动视图的边缘必须是绑在其子树之外的视图上。

请注意,您可以通过在视图和滚动视图的子树外部的视图(如滚动视图的超级视图)之间创建约束来使滚动视图的子视图浮动(不滚动)在其他滚动内容上。

下面是如何配置滚动​​视图,首先将混合的方法,然后将纯办法

混合的方法

位置和大小滚动视图与外部约束两个例子滚动视图 - 即translatesAutoresizingMaskIntoConstraints属性设置为NO。

为您的滚动视图创建一个普通的UIView内容视图,这将是您希望您的内容具有的大小。让它滚动视图的子视图,但让它继续翻译自动尺寸面罩进入限制:

- (void)viewDidLoad { 
UIView *contentView; 



contentView = [[UIView alloc] initWithFrame:CGRectMake(0,0,contentWidth,contentHeight)]; 



[scrollView addSubview:contentView]; 

//不要更改内容查看的translatesAutoresizingMaskIntoConstraints, //默认为YES;

//设置滚动视图的内容大小对内容视图的大小相匹配:

[scrollView setContentSize:CGSizeMake(contentWidth,contentHeight)]; 

/*的代码的其余部分在这里... */

} 

创建要放入内容视图中的视图并配置它们的约束,以便将它们放置在内容视图中。

或者,您可以创建视图子树以进入滚动视图,设置约束,并调用systemLayoutSizeFittingSize:方法(使用UILayoutFittingCompressedSize选项)以查找要用于内容视图的大小,以及

纯自动布局进场

要使用纯自动版式方法滚动视图的contentSize财产执行以下操作:

集translatesAut在涉及的所有视图上将“限制MaskInto约束”设置为“否”。 使用滚动视图外部的约束来定位和调整滚动视图。 使用约束来展示滚动视图中的子视图,确保约束与滚动视图的所有四个边连接,并且不依赖滚动视图来获取它们的大小。 一个简单的例子是一个大的图像视图,它具有从图像大小派生的内在内容大小。在您的视图控制器的viewDidLoad方法,您将包括类似于下面的列表中显示的代码代码:

- (void)viewDidLoad { 
UIScrollView *scrollView; 
UIImageView *imageView; 
NSDictionary *viewsDictionary; 

// Create the scroll view and the image view. 
scrollView = [[UIScrollView alloc] init]; 
imageView = [[UIImageView alloc] init]; 

// Add an image to the image view. 
[imageView setImage:[UIImage imageNamed:"MyReallyBigImage"]]; 

// Add the scroll view to our view. 
[self.view addSubview:scrollView]; 

// Add the image view to the scroll view. 
[scrollView addSubview:imageView]; 

// Set the translatesAutoresizingMaskIntoConstraints to NO so that the views autoresizing mask is not translated into auto layout constraints. 
scrollView.translatesAutoresizingMaskIntoConstraints = NO; 
imageView.translatesAutoresizingMaskIntoConstraints = NO; 

// Set the constraints for the scroll view and the image view. 
viewsDictionary = NSDictionaryOfVariableBindings(scrollView, imageView); 
[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]]; 

/* the rest of your code here... */ 
} 
2

这里有同样的问题。只需要在viewDidLayoutSubviews中调整scrollview的框架大小,这会覆盖自动布局。

-(void)viewDidLayoutSubviews 
{ 
    [super viewDidLayoutSubviews]; 

    [scrollView setContentSize:CGSizeMake(320, 2600)]; 

    // Adjust frame for iPhone 4s 
    if (self.view.bounds.size.height == 480) { 
     scrollView.frame = CGRectMake(0, 0, 320, 436); // 436 allows 44 for navBar 
    } 
} 
+1

我有AutoLayout-Size Classes的问题,向下兼容到iOS 7.这解决了我没有滚动问题。 – Beto 2015-03-23 20:19:02

+0

这个应该被标记为正确答案! – Ramis 2016-07-18 11:02:12