使用自动布局时的UIScrollView的中心内容 [英] Center contents of UIScrollview when using autolayout

查看:164
本文介绍了使用自动布局时的UIScrollView的中心内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中使用自动布局和我有在它中心的按钮滚动视图。我已经得到了滚动视图滚动,但不占用整个屏幕。

I'm using autolayout in my project and I have a scrollview that has a button centered in it. I have gotten the scrollview to scroll, but not to take up the entire screen.

我试图在这里按照教程: https://开头developer.apple.com/library/ios/technotes/tn2154/_index.html~~V ,以及我已签出在这里了几个类似的问题。问题是我想要的滚动视图的内容中心,而不是被固定到上/下/左/右。

I've tried to follow the tutorial here: https://developer.apple.com/library/ios/technotes/tn2154/_index.html, as well as I have checked out a few similar questions on here. The issue is that I want the contents of the scrollview to center, rather than be pinned to the top/bottom/left/right.

下面是我的看法层次:

能否请你帮我吗?

推荐答案

你有你的观点卷轴和超视图之间添加约束。这些约束条件决定了滚动视图的框架。你可以把滚动视图作为一个窗口,通过它可以看到后面的内容的。该帧是窗口的只是大小。当然,你可以滑动窗口,它给你的内容部分景点。

You have added constrains between your scroller view and its super view. These constrains determines the frame of the scroll view. You can think of the scroll view as a window, through which you can see the the contents behind. The frame is the just the size of the window. Of course, you can slide the window, which gives you the sights of parts of the contents.

现在你有窗口的大小,但你还需要告诉X code的内容的大小。如果你只是说在中间把一切都瞒着宽度,怎么能X $ C $ç知道哪里是中间?

Now you have the size of the window, but you also need to tell Xcode the size of the contents. If you just say put everything in the middle without telling the width, how can Xcode know where is the middle?

所以,你要告诉宽度(以及高度)。让我们想想一个简单的例子,如何在滚动视图中心的单一标签。

So, you have to tell the width (as well as height). Let's think about a simple example, how to center a single label in a scrollview.

您应该先针滚动视图其超视图的边缘,就像你做了什么。这之后,您应当在标签的边缘和滚动视图的边缘之间添加约束,除此之外,你也应该有和标签的高度限制添加。

You should first pin the edges of the scroll view to its super view just as what you did. After that you should add constraints between the edges of the label and the edges of the scroll view, besides, you should also add the with and the height constraints of the label.

最领先的空间,尾随空间和宽度一起得到与的内容,而顶部空间,底部空间和高度一起给予的内容的高度。

The leading space, the trailing space and the width together give the with of the contents, while the top space, bottom space and the height together give the height of the contents.

| ---领先的空间--- |标签宽度| ---尾部的空格--- |

|---leading space---|label width|---trailing space---|

| ----------内容的滚动视图的------------- |

|---------- content with of the scroll view -------------|

但是你可能要与内容视图等于与屏幕的设置,那么你需要做一些编程。在你设置有电源插座的属性code

But you may want to set the with of the content view equal to the with of your screen, then you need to do some programming. Set there outlet properties in you code

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *trailingSpace;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leadingSpace;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *width;

在你的 viewWillLayoutSubviews 做到这一点。

CGFloat contentWidth = self.view.frame.size.width;
CGFloat horiPadding = (contentWidth - self.width.constant) / 2;
_trailingSpace.constant = horiPadding;
_leadingSpace.constant = horiPadding;

现在,你的标签是在水平中心滚动查看!您的滚动视图已经知道用它的内容,所以你不必为你在你的滚动视图添加任何其他视图做到这一点:你可以添加中心的约束。

Now you the label is at the horizontal center of you scroll view! Your scroll view has already knew the with of its contents, so you don't need to do this for any other view you added in your scroll view: you can just add the center constraint.

您还可以做的高度是一样的。

You can also do the same thing for the height.

这篇关于使用自动布局时的UIScrollView的中心内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆