有人知道一个简单的方法来隐藏标签,让屏幕上的其他视图使用留空的地方吗?当你再次展示这一观点时,你会做出相反的反应。类似于Android setVisibility =层次分明。
据我所知,使用setHidden=true只对屏幕隐藏视图,而不对其周围进行任何重新排列。
谢谢
发布于 2016-05-20 07:04:25
在.GONE上实现Androids功能的唯一方法是使用UIStackView
通过苹果文献
动态更改堆栈视图的内容--每当视图被添加、移除或插入到arrangedSubviews数组中时,堆栈视图都会自动更新其布局,或者每当被安排的子视图的隐藏属性发生更改时。 SWIFT 3: //显示从堆栈中移除第一个排列的视图。//视图仍在堆栈中,只是不再可见,不再对布局做出贡献。设firstView = stackView.arrangedSubviews firstView.hidden = true SWIFT 4: 设firstView = stackView.arrangedSubviews firstView.isHidden = true
发布于 2016-07-04 19:42:32
您可以很容易地使用AutoLayout约束来实现这一点。
假设您有三个这样的视图:
+-----+
| A |
+-----+
+-----+
| B |
+-----+
+-----+
| C |
+-----+
你想让B视图在某些情况下消失。
设置约束如下(这些只是示例值):
B top space to A: 4
C top space to B: 4
B height: 20
然后在代码中为B的高度创建一个NSLayoutConstraint出口。通过在IB中拖动和删除约束来做到这一点。
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bHeight;
最后,要使视图消失,只需执行以下操作:
self.bHeight = 0;
请注意,如果您正在为tableview单元格执行此操作,您可能会在某些单元格中出现B,而在其他单元格中却不出现B。
在这种情况下,您必须将高度重置为其“正常”值,以便使这些单元格可见。
self.bHeight = 24;
发布于 2017-09-26 19:41:10
我在寻找简单的解决方案并找到了它。我不需要使用UIStackView或为约束创建出口。就用这个吧:
class GoneConstraint {
private var constraint: NSLayoutConstraint
private let prevConstant: CGFloat
init(constraint: NSLayoutConstraint) {
self.constraint = constraint
self.prevConstant = constraint.constant
}
func revert() {
self.constraint.constant = self.prevConstant
}
}
fileprivate struct AssociatedKeys {
static var widthGoneConstraint: UInt8 = 0
static var heightGoneConstraint: UInt8 = 0
}
@IBDesignable
extension UIView {
@IBInspectable
var gone: Bool {
get {
return !self.isHidden
}
set {
update(gone: newValue)
}
}
weak var widthConstraint: GoneConstraint? {
get {
return objc_getAssociatedObject(self, &AssociatedKeys.heightGoneConstraint) as? GoneConstraint
}
set(newValue) {
objc_setAssociatedObject(self, &AssociatedKeys.widthGoneConstraint, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
weak var heightConstraint: GoneConstraint? {
get {
return objc_getAssociatedObject(self, &AssociatedKeys.heightGoneConstraint) as? GoneConstraint
}
set(newValue) {
objc_setAssociatedObject(self, &AssociatedKeys.heightGoneConstraint, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
private func update(gone: Bool) {
isHidden = gone
if gone {
for constr in self.constraints {
if constr.firstAttribute == NSLayoutAttribute.width {
self.widthConstraint = GoneConstraint(constraint: constr)
}
if constr.firstAttribute == NSLayoutAttribute.height {
self.heightConstraint = GoneConstraint(constraint: constr)
}
constr.constant = 0
}
} else {
widthConstraint?.revert()
heightConstraint?.revert()
}
}
}
现在,你可以打电话给view.gone = true
,仅此而已。
https://stackoverflow.com/questions/37339793
复制相似问题