gpt4 book ai didi

ios - Swift iOS - 如何设置 UIView 的高度 anchor <= 到标签的固有文本大小? 'NSLayoutConstraint' 不可转换为 'Bool'

转载 作者:可可西里 更新时间:2023-10-31 23:58:34 35 4
gpt4 key购买 nike

我有一个程序 View ,其中有一个标签,我将其固定到导航栏的底部。标签内将有动态文本,如果文本高度使其变小,我希望标签所在的 View 至少为 64 磅或更大。

此标签文本的固有大小将 View 设置在一个明显的高度。

setViewAndLabel(dynamicText: "Unknown Error\nPlease try your request again\Error: 123")

enter image description here

然而,此文本的固有大小使高度变小了:

setViewAndLabel(dynamicText: "Message Deleted!")

enter image description here

Message Deleted! 应该更符合以下内容:

enter image description here

我使用了一些返回键来设置它,但我认为这不是正确的方法,因为会生成不同的消息:

setViewAndLabel(dynamicText: "\nMessage Deleted!\n")

我也试过:

if myView.heightAnchor.constraint(lessThanOrEqualToConstant: 64){
myView.heightAnchor.constraint(equalToConstant: 64).isActive = true
}

但是我得到了错误:

'NSLayoutConstraint' is not convertible to 'Bool'

将标签所在 View 的高度设置为最小高度的最佳方法是什么?

var myLabel: UILabel(){
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.textColor = UIColor.white
label.font = UIFont(name: "Helvetica-Regular", size: 19)
label.numberOfLines = 0
label.sizeToFit()
label.textAlignment = .center
return label
}

let myView:UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()

override func viewWillAppear(_ animated: Bool)
super.viewWillAppear(animated){

setViewAndLabel(dynamicText: //some text will get set here)
}

func setViewAndLabel(dynamicText: String){
view.addSubView(myView)
myView.backgroundColor = UIColor.red
myView.topAnchor.constraint(equalTo: view.topAnchor, constant: 64).isActive = true
View.widthAnchor.constraint(equalTo: view.widthAnchor, constant: 0).isActive = true

myView.addSubView(myLabel)
myLabel.text = dynamicText
myLabel.topAnchor.constraint(equalTo: myView.topAnchor, constant: 0).isActive = true
myLabel.widthAnchor.constraint(equalTo: myView.widthAnchor, constant: 0).isActive = true
myView.bottomAnchor.constraint(equalTo: myLabel.bottomAnchor, constant: 0).isActive = true

//this if statement doesn't work
if myView.heightAnchor.constraint(lessThanOrEqualToConstant: 64){
viewForErrorLabel.heightAnchor.constraint(equalToConstant: 64).isActive = true
}
}

最佳答案

这是设置约束的方式:

view.addSubview(myView)
myView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true
myView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
myView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true

myView.heightAnchor.constraint(greaterThanOrEqualToConstant: 64).isActive = true

myView.addSubview(myLabel)
myLabel.topAnchor.constraint(equalTo: myView.topAnchor).isActive = true
myLabel.leadingAnchor.constraint(equalTo: myView.leadingAnchor).isActive = true
myLabel.trailingAnchor.constraint(equalTo: myView.trailingAnchor).isActive = true
myLabel.bottomAnchor.constraint(equalTo: myView.bottomAnchor).isActive = true

您根本不需要检查标签的高度。您可以简单地始终为 myView 创建高度大于或等于约束,并且它的高度永远不会小于 64pt(或您设置的任何值)——即使标签包含非常短的文本。

关于ios - Swift iOS - 如何设置 UIView 的高度 anchor <= 到标签的固有文本大小? 'NSLayoutConstraint' 不可转换为 'Bool',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46206686/

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