简体   繁体   English

IOS Swift中一起限制进度条和标签

[英]Constraints for progress bar and label together in IOS Swift

I need to show a progress bar and a label adjacent to each other horizontally based on some condition and based on a different condition, I need to show label and progress bar one below the other vertically. 我需要基于某种条件和基于不同的条件水平显示彼此相邻的进度条和标签,我需要垂直显示标签和进度条一个在另一个下方。 Third condition is I hide the label and show only the progress bar. 第三个条件是我隐藏标签并仅显示进度栏。 For the third condition, I want the progress bar to occupy the full width of the screen - some space on either end of it. 对于第三个条件,我希望进度条占据屏幕的整个宽度-屏幕两端的空间。 Initially, I started by putting constraints to show them side by side and it worked. 最初,我首先放置约束以使其并排显示,并且它起作用了。 However, for the other two conditions when I try to modify existing constraints during run time, it is messing up and not working as expected. 但是,对于其他两个条件,当我尝试在运行时修改现有约束时,它变得混乱并且无法按预期工作。 Any pointers on how the constraints should look like for these scenarios. 在这些情况下,有关约束外观的任何指示。 Attached is an image of a progress bar and label side by side 附件是进度条和标签并排的图像 在此处输入图片说明

I need to show a progress bar and a label adjacent to each other horizontally based on some condition and based on a different condition 我需要根据某些条件和不同条件水平显示彼此相邻的进度条和标签

For this purpose 以此目的

Progress bar constraints 进度条约束

在此处输入图片说明

Label Constraints 标签约束

在此处输入图片说明

I need show label and progress bar one below the other vertically 我需要在垂直下方显示标签和进度条

For this purpose 以此目的

Progress bar constraints 进度条约束

在此处输入图片说明

Label Constraints 标签约束

在此处输入图片说明

Third condition is I hide the label and show only the progress bar. 第三个条件是我隐藏标签并仅显示进度栏。 For the third condition, I want the progress bar to occupy full width of the screen - some space on either ends of it 对于第三个条件,我希望进度条占据屏幕的整个宽度-在其两端都有一些空间

This one is bit tricky but not much difficult, Set constraints according to our first problem, and add one extra constraint along with others, Trailing Space to superview, Stroyboard will show you error, don't worry. 这个有点棘手,但难度不大,根据我们的第一个问题设置约束,并与其他约束一起添加一个额外的约束,Trailing Space进行超级查看,Stroyboard会向您显示错误,请放心。 Create an outlet for Progressbar trailing space both constraints now in your Swift/objc file and on runtime activate one constraint and deactivate other according to your logic. 现在为Swiftbar / objc文件中的两个约束创建Progressbar尾随空间的出口,并在运行时根据您的逻辑激活一个约束并停用另一个约束。 Along with showing/hiding your label. 以及显示/隐藏标签。 Like this 像这样

    NSLayoutConstraint.isActive = false; // or true. Here NSLayoutConstraint is your reference to constraint outlet.

Add a horizontal UIStackview and add a UIProgressView and a UILabel. 添加一个水平的UIStackview并添加一个UIProgressView和一个UILabel。 Set label text alignment to center. 将标签文本对齐方式设置为居中。

Add top, bottom, leading, trailing constraints to the stackview. 在堆栈视图中添加顶部,底部,前导,尾随约束。 Don't add height constraint to the stackview. 不要在堆栈视图中添加高度限制。 Add height constraint to the label. 向标签添加高度限制。

Change stackview configuration as 将stackview配置更改为

在此处输入图片说明

And change the styles by changing stackview's axis and hiding/showing the label as follows 并通过更改stackview的轴并隐藏/显示标签来更改样式,如下所示

@IBAction func style1(_ sender: Any) {
    stackView.axis = .horizontal
    stackView.alignment = .center
    label.isHidden = false
}
@IBAction func style2(_ sender: Any) {
    stackView.axis = .vertical
    stackView.alignment = .fill
    label.isHidden = false
}
@IBAction func style3(_ sender: Any) {
    stackView.axis = .horizontal
    stackView.alignment = .center
    label.isHidden = true
}

在此处输入图片说明

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

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