如何在xib cocoa objective-c中实现滚动视图

时间:2022-09-02 08:46:29

In order to implement scroll view I do:

为了实现滚动视图我做:

  1. Create Cocoa Application
  2. 创建Cocoa应用程序

  3. Go to XIB
  4. 转到XIB

  5. Drag scroll view to the view window and set its constraints to 0
  6. 将滚动视图拖动到视图窗口并将其约束设置为0

Everything seems fine until now

直到现在一切似乎都很好

  1. Under Bordered Scroll View (in the Document Outline) I press on Clip View and then View and resize that to any large number (under size inspector)
  2. 在“有边滚动视图”下(在“文档大纲”中)按下剪辑视图,然后查看并将其调整为任意大数(调整大小不足)

  3. I add a button (for the sake of it) to the view (under clip view) (in the Document Outline) and sets its constraints
  4. 我将一个按钮(为此)添加到视图(在剪辑视图下)(在文档大纲中)并设置其约束

After this I immediately get the "Ambigious Layout. Position is ambigious for "View".

在此之后,我立刻得到了“Ambigious Layout。位置对于”View“来说是个暧昧的。

What am I doing wrong? Is this the proper way to add scrollview? It also seems rather difficult to add items to the scrollable area as I dont see the entire scrollable area in the xib.

我究竟做错了什么?这是添加scrollview的正确方法吗?将项目添加到可滚动区域似乎相当困难,因为我没有看到xib中的整个可滚动区域。

Please help a noob.

请帮助一个菜鸟。

2 个解决方案

#1


In general this is a correct way to add a scroll view. (You can also create a view or set of sibling views, select them, and choose Editor > Embed In > Scroll View.)

通常,这是添加滚动视图的正确方法。 (您还可以创建一个视图或一组兄弟视图,选择它们,然后选择“编辑器”>“嵌入”>“滚动视图”。)

If a view has no constraints, then Xcode will add sufficient constraints at build time. These constraints are not necessarily the ones that cause the view to behave like you want as things change size, but they're good enough to maintain the current layout of the canvas when things have their current sizes.

如果视图没有约束,那么Xcode将在构建时添加足够的约束。这些约束不一定是导致视图在事物改变大小时按照您的需要运行的约束,但它们足以在事物具有当前大小时维持画布的当前布局。

However, once you add constraints, Xcode will start insisting that the constraints are mutually-compatible (no conflicts) and sufficient to be unambiguous.

但是,一旦添加约束,Xcode就会开始坚持约束是相互兼容的(没有冲突)并且足以明确。

So, that explains why you get that warning. You have added some constraints, but not enough to make the layout unambiguous. You need to add enough. Xcode should explain in more detail what's needed, although there will necessarily be multiple possibilities for how to resolve the ambiguity.

所以,这就解释了为什么你会收到警告。您添加了一些约束,但还不足以使布局明确无误。你需要添加足够的。 Xcode应该更详细地解释需要什么,尽管如何解决模糊性必然存在多种可能性。

In your case, I'm guessing that the size of the view in the scroll view is ambiguous. For example, you may have added constraints to position the button relative to the top and leading edge, and the button likely has intrinsic size, but you haven't constrained the view's bottom or trailing edges to the button. So, the size of the view could be anything.

在你的情况下,我猜测滚动视图中视图的大小是不明确的。例如,您可能已添加约束来相对于顶部和前缘定位按钮,并且按钮可能具有固有大小,但您没有将视图的底部或尾部边缘约束到按钮。因此,视图的大小可以是任何东西。

Of course, rather than constraining the view's bottom and trailing edge to the button, you could just add explicit height and width constraints to it. Or whatever.

当然,不是将视图的底部和后缘约束到按钮,而是可以向其添加显式的高度和宽度约束。管他呢。

You may also need to constrain the view to the clip view.

您可能还需要将视图约束到剪辑视图。

You can also use Editor > Resolve Auto Layout Issues > Add Missing Constraints and see what Xcode adds. You can then change things from there if what Xcode added is not what you want.

您还可以使用编辑器>解决自动布局问题>添加缺失约束并查看Xcode添加的内容。如果Xcode添加的内容不是您想要的,那么您可以从那里更改内容。

#2


For my Mac OS X app, I selected the controls on xib to embed in scroll view ( Editor > Embed In > Scroll View ) and applying the following constraints to Custom View (inside Scroll View -> Clip View) did work.

对于我的Mac OS X应用程序,我选择了xib上的控件以嵌入滚动视图(编辑器>嵌入>滚动视图),并将以下约束应用于自定义视图(在滚动视图 - >剪辑视图内)确实有效。

如何在xib cocoa objective-c中实现滚动视图

Where hight is to accommodate controls.

高度适合控制。

#1


In general this is a correct way to add a scroll view. (You can also create a view or set of sibling views, select them, and choose Editor > Embed In > Scroll View.)

通常,这是添加滚动视图的正确方法。 (您还可以创建一个视图或一组兄弟视图,选择它们,然后选择“编辑器”>“嵌入”>“滚动视图”。)

If a view has no constraints, then Xcode will add sufficient constraints at build time. These constraints are not necessarily the ones that cause the view to behave like you want as things change size, but they're good enough to maintain the current layout of the canvas when things have their current sizes.

如果视图没有约束,那么Xcode将在构建时添加足够的约束。这些约束不一定是导致视图在事物改变大小时按照您的需要运行的约束,但它们足以在事物具有当前大小时维持画布的当前布局。

However, once you add constraints, Xcode will start insisting that the constraints are mutually-compatible (no conflicts) and sufficient to be unambiguous.

但是,一旦添加约束,Xcode就会开始坚持约束是相互兼容的(没有冲突)并且足以明确。

So, that explains why you get that warning. You have added some constraints, but not enough to make the layout unambiguous. You need to add enough. Xcode should explain in more detail what's needed, although there will necessarily be multiple possibilities for how to resolve the ambiguity.

所以,这就解释了为什么你会收到警告。您添加了一些约束,但还不足以使布局明确无误。你需要添加足够的。 Xcode应该更详细地解释需要什么,尽管如何解决模糊性必然存在多种可能性。

In your case, I'm guessing that the size of the view in the scroll view is ambiguous. For example, you may have added constraints to position the button relative to the top and leading edge, and the button likely has intrinsic size, but you haven't constrained the view's bottom or trailing edges to the button. So, the size of the view could be anything.

在你的情况下,我猜测滚动视图中视图的大小是不明确的。例如,您可能已添加约束来相对于顶部和前缘定位按钮,并且按钮可能具有固有大小,但您没有将视图的底部或尾部边缘约束到按钮。因此,视图的大小可以是任何东西。

Of course, rather than constraining the view's bottom and trailing edge to the button, you could just add explicit height and width constraints to it. Or whatever.

当然,不是将视图的底部和后缘约束到按钮,而是可以向其添加显式的高度和宽度约束。管他呢。

You may also need to constrain the view to the clip view.

您可能还需要将视图约束到剪辑视图。

You can also use Editor > Resolve Auto Layout Issues > Add Missing Constraints and see what Xcode adds. You can then change things from there if what Xcode added is not what you want.

您还可以使用编辑器>解决自动布局问题>添加缺失约束并查看Xcode添加的内容。如果Xcode添加的内容不是您想要的,那么您可以从那里更改内容。

#2


For my Mac OS X app, I selected the controls on xib to embed in scroll view ( Editor > Embed In > Scroll View ) and applying the following constraints to Custom View (inside Scroll View -> Clip View) did work.

对于我的Mac OS X应用程序,我选择了xib上的控件以嵌入滚动视图(编辑器>嵌入>滚动视图),并将以下约束应用于自定义视图(在滚动视图 - >剪辑视图内)确实有效。

如何在xib cocoa objective-c中实现滚动视图

Where hight is to accommodate controls.

高度适合控制。