gpt4 book ai didi

swift - 如何在 swift 中隐藏文本字段并自动布局约束

转载 作者:行者123 更新时间:2023-11-30 11:21:47 28 4
gpt4 key购买 nike

所以我的项目中有一个分段控件。我想隐藏并自动布局我的按钮。但是如果我只是隐藏文本字段并在分段控件下标记按钮,问题仍然不会移动。所以之间有一个空格我的分段控件和下一个按钮。如果我在分段控件上单击“否”,我想将下一个按钮向上移动。

-如果我在分段控制上单击"is"分段控件下的下一个按钮将限制我的文本字段并显示分段控件下的所有文本字段和标签

-如果我在分段控制上单击“否”分段控件下的下一个按钮将约束到我的分段控件并隐藏分段控件下的所有文本字段和标签

谢谢大家的回答

the image

最佳答案

有关如何根据分段开关更改按钮位置的快速示例。

下图显示了在分段开关和按钮之间拖动自动布局高度 Image to show the outlet which is created

下图显示使用 socket 并且类型需要为 NSLayoutConstraint Image to show the settings for outlet

在 ViewController.Swift 中,代码应如下所示。

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var switchControl: UISegmentedControl!

@IBOutlet weak var heighConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

@IBAction func switchPressed(_ sender: Any) {
switch switchControl.selectedSegmentIndex
{
case 0:
// here you can see the constant is being set, this will determine the
// position of your button. You will need to set the correct position.
heighConstraint.constant = 150
case 1:
heighConstraint.constant = 200
default:
break;
}

}
}

希望这个答案有帮助。

关于swift - 如何在 swift 中隐藏文本字段并自动布局约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51186857/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com