隐藏标签中的文本时尝试调整 UIView 的大小
Trying to resize an UIView when text in a label is hidden
@IBOutlet weak var CompanyDetailsBottom: NSLayoutConstraint!
@IBOutlet weak var CompanyDetailsView: UIView!
@IBAction func toggleCollapisbleView(_ sender: UIButton) {
if(CompanyDetailsView.isHidden){
var _CompanyHeight: CGFloat {
get {
return CompanyDetailsBottom.constant.magnitude
}
set {
CompanyDetailsBottom.constant.magnitude = 0
}
}
CompanyDetailsView.isHidden = false
}else {
CompanyDetailsView.isHidden = true
}
}
}
如果标签被隐藏,我正在尝试调整视图大小,我得到无法分配给 属性: 'magnitude' is a get-only 属性 作为错误。
您可以使用下面的扩展来隐藏视图,其父级将调整其高度
extension UIView {
var isGone: Bool {
get {
return isHidden
}
set {
let constraints = self.constraints.filter({ [=10=].firstAttribute == .height && [=10=].constant == 0 && [=10=].secondItem == nil && ([=10=].firstItem as? UIView) == self })
self.isHidden = newValue
if newValue {
if let constraint = constraints.first {
constraint.isActive = true
} else {
let constraint = NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: 0)
// constraint.priority = UILayoutPriority(rawValue: 999)
self.addConstraint(constraint)
constraint.isActive = true
}
self.setNeedsLayout()
self.setNeedsUpdateConstraints()
} else {
constraints.first?.isActive = false
}
}
}
}
并且子视图的所有约束优先级必须小于1000(最大999)
使用:
childView.isGone = true
如果你不想使用约束,你可以做的是将你的 CompanyDetailsView
嵌入垂直 UIStackView
中,然后当你隐藏它时,堆栈视图的高度将自动为0.
UILabel
是否嵌入在 UIView
中?如果是这样,您可以将 UIView
改为 UIStackView
,这样如果标签被隐藏,高度将为 0
@IBOutlet weak var CompanyDetailsBottom: NSLayoutConstraint!
@IBOutlet weak var CompanyDetailsView: UIView!
@IBAction func toggleCollapisbleView(_ sender: UIButton) {
if(CompanyDetailsView.isHidden){
var _CompanyHeight: CGFloat {
get {
return CompanyDetailsBottom.constant.magnitude
}
set {
CompanyDetailsBottom.constant.magnitude = 0
}
}
CompanyDetailsView.isHidden = false
}else {
CompanyDetailsView.isHidden = true
}
}
}
如果标签被隐藏,我正在尝试调整视图大小,我得到无法分配给 属性: 'magnitude' is a get-only 属性 作为错误。
您可以使用下面的扩展来隐藏视图,其父级将调整其高度
extension UIView {
var isGone: Bool {
get {
return isHidden
}
set {
let constraints = self.constraints.filter({ [=10=].firstAttribute == .height && [=10=].constant == 0 && [=10=].secondItem == nil && ([=10=].firstItem as? UIView) == self })
self.isHidden = newValue
if newValue {
if let constraint = constraints.first {
constraint.isActive = true
} else {
let constraint = NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: 0)
// constraint.priority = UILayoutPriority(rawValue: 999)
self.addConstraint(constraint)
constraint.isActive = true
}
self.setNeedsLayout()
self.setNeedsUpdateConstraints()
} else {
constraints.first?.isActive = false
}
}
}
}
并且子视图的所有约束优先级必须小于1000(最大999)
使用:
childView.isGone = true
如果你不想使用约束,你可以做的是将你的 CompanyDetailsView
嵌入垂直 UIStackView
中,然后当你隐藏它时,堆栈视图的高度将自动为0.
UILabel
是否嵌入在 UIView
中?如果是这样,您可以将 UIView
改为 UIStackView
,这样如果标签被隐藏,高度将为 0