xcode 在水平堆栈视图中居中对齐开关

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44591584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:03:24  来源:igfitidea点击:

Center aligning a switch in Horizontal Stack View

iosswiftxcode

提问by Human Cyborg Relations

Note: I'm completely new to iOS development.

注意:我对 iOS 开发完全陌生。

In storyboard, I've got a Vertical Stack View with a Horizontal Stack View inside.

在故事板中,我有一个垂直堆栈视图,里面有一个水平堆栈视图。

In the Horizontal Stack View, I've got a label and a switch. I changed the Distribution of both stack views to "Fill Equally".

在水平堆栈视图中,我有一个标签和一个开关。我将两个堆栈视图的分布更改为“均等填充”。

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

Currently, this is what the view looks like:

目前,这就是视图的样子:

enter image description here

在此处输入图片说明

The label is aligned perfectly both horizontally and vertically, but the switch is in the wrong position. I also want the switch to be aligned like the label, so that it appears on the right of the label and on the same vertical level.

标签在水平和垂直方向上都完美对齐,但开关位置错误。我还希望开关像标签一样对齐,以便它出现在标签的右侧和同一垂直水平上。

I've tried various options with storyboard but nothing seems to work.

我已经尝试了故事板的各种选项,但似乎没有任何效果。

回答by MH175

  1. Set horizontal stackView alignment to center and distribution to fill.
  2. Set the switch horizontal content hugging priority to required.
  1. 将水平 stackView 对齐设置为居中并将分布设置为填充。
  2. 将开关水平内容拥抱优先级设置为需要。

回答by Darren Black

  1. Set the alignment of the horizontal UIStackView to Center.
  2. Embed the switch in a vertical UIStackView (inside the horizontal UIStackView).
  3. Set the alignment of the new UIStackView to Center.
  1. 将水平 UIStackView 的对齐方式设置为 Center。
  2. 将开关嵌入垂直 UIStackView(在水平 UIStackView 内)。
  3. 将新 UIStackView 的对齐方式设置为 Center。

回答by Marc

The problem is that the switch itself, as the size changes, is always in the top left corner. Create a view as a container, and put the switch inside that view, then set the horizontal and vertical constraints for the switch to be centered relative to that view (CTRL + drag from the switch to it's parent view, and select 'center horizontal

问题是开关本身,随着尺寸的变化,总是在左上角。创建一个视图作为容器,然后将开关放在该视图内,然后将开关的水平和垂直约束设置为相对于该视图居中(CTRL + 从开关拖动到其父视图,然后选择“水平居中”

回答by jalone

If you add a height constraint to the UILabel you will see that it will behave exactly the same as the UISwitch. The UILabel can fill the container while the switch has a "natural" height constraint.

如果向 UILabel 添加高度约束,您将看到它的行为与 UISwitch 完全相同。UILabel 可以填充容器,而开关具有“自然”高度约束。

  • If you want to align both the view to the center and have more switch-label pairs one under the other, create a container view for each pair and put them directly in the first StackView or change the parent (vertical) StackView configuration (since there is where you issue actually is)
  • If the expected result is other detail it in the question
  • 如果要将视图与中心对齐并在另一个下方有更多的开关标签对,请为每一对创建一个容器视图并将它们直接放在第一个 StackView 中或更改父(垂直)StackView 配置(因为有是您实际发布的地方)
  • 如果预期结果是问题中的其他细节

回答by AgRizzo

You don't need a vertical stack. Here's what I recommend as a tutorial / learning experience.

您不需要垂直堆栈。这是我推荐的教程/学习体验。

  1. Start all over.
  2. Create a view that is the area where you want the label and switch to be. Make sure you have the constraints set. (Change the background color so that you can see it better.)
  3. Within that view, put the label and switch.
  4. Highlight the label and switch, and select the horizontal stackview.
  5. Set the constraints of the horizontal stack to its superview (i.e., the view you created in step 2)
  6. Configure the horizontal stackview until happy or come back to SO with a new question
  1. 重新开始。
  2. 创建一个视图,该视图是您希望标签和开关所在的区域。确保您设置了约束。(更改背景颜色,以便您可以更好地看到它。)
  3. 在该视图中,放置标签并切换。
  4. 突出显示标签并切换,然后选择水平堆栈视图。
  5. 将水平堆栈的约束设置为其超视图(即您在步骤 2 中创建的视图)
  6. 配置水平堆栈视图直到满意或带着新问题回到 SO

回答by nitish005

It's simple.

这很简单。

your horizontal stackViewwill contain UILabeland UIView(let's call it vSwitchContainer).

你的horizontal stackView遗嘱包含UILabelUIView(我们称之为vSwitchContainer)。

Now place your switch inside vSwitchContainer. assign constraints for horizontally and vertically centre for UISwitch

现在把你的开关放在里面vSwitchContainer。为水平和垂直中心分配约束UISwitch

回答by Nedunchezhian A

In Storyboard distribution will be set fill insted of fill equaly and add uiview in the stack view.Then set width for label and UISwitch.

在Storyboard分发中将设置fill insted of fill equaly并在stack view中添加uiview。然后设置label和UISwitch的宽度。