如何像 IOS 中的 android 那样设置可见性 GONE?
how to set visibility GONE like android in IOS?
有人知道隐藏标签并让屏幕的其他视图使用留空的地方的简单方法吗?并在展示该视图时做相反的事情。类似于 Android setVisibility = GONE for layers.
据我所知,使用 setHidden=true 只会从屏幕上隐藏视图,但不会重新排列它周围的任何内容。
谢谢
如果您的应用支持 ios 9 及更高版本,您可以使用 UIStackView。
但是如果你的应用程序支持 ios 8 也比你必须使用自动布局来实现它并为视图添加高度约束
所以如果你想隐藏而不只是设置高度限制值 0。
在 iOS 上实现 Android .GONE 功能的唯一方法是使用 UIStackView
Dynamically Changing the Stack View’s Content The stack view
automatically updates its layout whenever views are added, removed or
inserted into the arrangedSubviews array, or whenever one of the
arranged subviews’s hidden property changes.
SWIFT 3:
// Appears to remove the first arranged view from the stack.
// The view is still inside the stack, it's just no longer visible, and no longer contributes to the layout.
let firstView = stackView.arrangedSubviews[0]
firstView.hidden = true
SWIFT 4:
let firstView = stackView.arrangedSubviews[0]
firstView.isHidden = true
您可以使用 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;
请注意,如果您为表格视图单元格执行此操作,您可能会遇到希望 B 出现在某些单元格中而不出现在其他单元格中的情况。
在这种情况下,对于您希望它可见的那些单元格,您必须将高度重置为其 "normal" 值。
self.bHeight = 24;
我一直在寻找简单的解决方案并找到了它。我不必使用 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
就这样。
有人知道隐藏标签并让屏幕的其他视图使用留空的地方的简单方法吗?并在展示该视图时做相反的事情。类似于 Android setVisibility = GONE for layers.
据我所知,使用 setHidden=true 只会从屏幕上隐藏视图,但不会重新排列它周围的任何内容。
谢谢
如果您的应用支持 ios 9 及更高版本,您可以使用 UIStackView。
但是如果你的应用程序支持 ios 8 也比你必须使用自动布局来实现它并为视图添加高度约束
所以如果你想隐藏而不只是设置高度限制值 0。
在 iOS 上实现 Android .GONE 功能的唯一方法是使用 UIStackView
Dynamically Changing the Stack View’s Content The stack view automatically updates its layout whenever views are added, removed or inserted into the arrangedSubviews array, or whenever one of the arranged subviews’s hidden property changes.
SWIFT 3:
// Appears to remove the first arranged view from the stack. // The view is still inside the stack, it's just no longer visible, and no longer contributes to the layout. let firstView = stackView.arrangedSubviews[0] firstView.hidden = true
SWIFT 4:
let firstView = stackView.arrangedSubviews[0] firstView.isHidden = true
您可以使用 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;
请注意,如果您为表格视图单元格执行此操作,您可能会遇到希望 B 出现在某些单元格中而不出现在其他单元格中的情况。
在这种情况下,对于您希望它可见的那些单元格,您必须将高度重置为其 "normal" 值。
self.bHeight = 24;
我一直在寻找简单的解决方案并找到了它。我不必使用 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
就这样。