简体   繁体   English

按钮填充堆栈视图

[英]Button filling up stack view

I'm following the dev tutorial from Apple on iOS, and got to the part where you have to implement a custom control using a stack view.我正在关注 iOS 上 Apple 的开发教程,并且到了必须使用堆栈视图实现自定义控件的部分。 However, when I get to the part where it should show me a little red square referring to the button I added to the stack view, I get the whole stack view in red (the button fills the entire stack view).但是,当我到达应该向我显示一个小红色方块的部分时,它指的是我添加到堆栈视图中的按钮,我将整个堆栈视图显示为红色(该按钮填充了整个堆栈视图)。 My attributes are the following:我的属性如下:

属性

What am I doing wrong, despite following the tutorial to the letter?尽管按照教程进行了操作,但我做错了什么? Thank you in advance.先感谢您。

Edit:编辑:

I use these constraints:我使用这些约束:

    button.translatesAutoresizingMaskIntoConstraints = false
    button.heightAnchor.constraint(equalToConstant: 24.0).isActive = true
    button.widthAnchor.constraint(equalToConstant: 14.0).isActive = true

This is the expected behavior of a stack view with one arranged subview.这是具有一个排列子视图的堆栈视图的预期行为。 If you have placed constraints on the stack view that make it larger than its arranged subviews those subviews will expand to fill the size of the stack view.如果您对堆栈视图施加了限制,使其大于其排列的子视图,则这些子视图将扩展以填充堆栈视图的大小。 Or if the stack view has no constraints on it, it will conform to the size dictated by its arranged subviews.或者如果堆栈视图对其没有约束,它将符合其排列的子视图规定的大小。 Either way with one arranged subview that subview will take up the entirety of the stack view.无论哪种方式,一个排列的子视图都将占据整个堆栈视图。

In the tutorial, there are no constraints on the stack view so it shrinks to the size of the constraints set on the button, as it is the only arranged subview in the UIStackView .在本教程中,堆栈视图没有约束,因此它缩小到按钮上设置的约束的大小,因为它是UIStackView唯一排列的子视图。 You should open the size inspector stack view to ensure there are no constraints set on it.您应该打开大小检查器堆栈视图以确保没有对其设置约束。 For more about stack views and layouts you should see here.有关堆栈视图和布局的更多信息,您应该在此处查看。

Alright folks I got it.好吧,伙计们,我明白了。 I had the horizontal stack in the wrong place in the view.我在视图中的错误位置放置了水平堆栈。 I had the same issue and I used the alignment -> top but that only fixed my horizontal not my width.我遇到了同样的问题,我使用了对齐 -> 顶部,但这只修复了我的水平而不是我的宽度。 The true issue was that I placed the Horizontal Stack View in the wrong location.真正的问题是我将水平堆栈视图放置在错误的位置。 It says "and drag one into your storyboard scene so that it's in the stack view below the image view" when I placed it as shown it sized correctly.当我按正确大小放置它时,它说“并将其拖到您的故事板场景中,以便它位于图像视图下方的堆栈视图中”。 correct location for horizontal stack水平堆叠的正确位置

I realise that an answer has been accepted for this question, however, I came across the same problem when following the tutorial.我意识到这个问题的答案已经被接受,但是,我在学习教程时遇到了同样的问题。 Whether or not this is the expected behaviour, following the tutorial exactly results in different looking interfaces on running the app in the simulator.无论这是否是预期的行为,按照教程在模拟器中运行应用程序时会产生不同外观的界面。

Something is not quite right with the constraints and so the constraints set in setupButtons() were not being implemented.约束条件不太正确,因此没有实现setupButtons()中设置的约束条件。

button.heightAnchor.constraint(equalToConstant: 20.0).isActive = true
button.widthAnchor.constraint(equalToConstant: 20.0).isActive = true

To solve this I had to change the stack view allignment attribute from fill to top as shown below:为了解决这个问题,我必须将堆栈视图对齐属性从fill更改为top ,如下所示:

在此处输入图片说明

This fixes the interface so that it's the same as the tutorial screenshots.这修复了界面,使其与教程屏幕截图相同。 Hopefully this helps anyone else coming across this issue in the future.希望这可以帮助其他人在未来遇到这个问题。

In recent versions of Xcode (v 9.0.1 as or writing) it will complain in the debugger output.在最新版本的 Xcode(v 9.0.1 as or writing)中,它会在调试器输出中抱怨。 It mentions that you may have an unwanted constraint.它提到您可能有一个不需要的约束。 If you remove the constraints from the storyboard, the code should work as intended.如果您从情节提要中删除约束,则代码应按预期工作。

I follow the tutorial too and I had the same Problem.我也按照教程进行操作,但遇到了同样的问题。 I solved it, by deactivating the red contrains in the marked screenshoot.我通过停用标记屏幕截图中的红色限制来解决它。 I uploaded another screenshoot, to show you my other settings.我上传了另一个屏幕截图,向您展示我的其他设置。 I am Beginner and it was totaly luck and I want to share this luck^^ other settings marked screenshoot我是初学者,这完全是运气,我想分享这个运气^^其他设置标记为屏幕截图

enter image description here在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM