UIView-Visibility
UIView-Visibility copied to clipboard
An Objective-C category that provides a method for setting visibility of views, like how people do in the Android world.
Note
This library will NOT be updated except for merging pull requests, becase I don't have enough spare time. You can still use it if works for you, I will be thankful that you contribute back by sending pull requests for bugs that may be found. (2018.06.30)
UIView+Visibility
In iOS, there is no intuitive support for truly hiding a view with AutoLayout, setting UIView.hidden=YES only causes the view to be invisible, the space is still occupied by the hidden view, in most cases this is not desired. In Android, there is View.setVisibility(View.GONE) to truly hide a view, this category implements a setVisibility method for the same feature.

Usage
#import "UIView+Visibility.h"
// hide the view without clearing its margins
[UIView setVisibility:UIViewVisibilityGone];
// hide the view and clear its top & bottom margins
[UIView setVisibility:UIViewVisibilityGone
affectedMarginDirections:UIViewMarginDirectionTop|UIViewMarginDirectionBottom];
// show the view and restore its top & bottom margins
[UIView setVisibility:UIViewVisibilityVisible
affectedMarginDirections:UIViewMarginDirectionTop|UIViewMarginDirectionBottom];
Following are the enums that are needed to work with the setVisibility method:
typedef NS_ENUM(NSInteger, UIViewVisibility) {
UIViewVisibilityVisible,
UIViewVisibilityInvisible,
UIViewVisibilityGone
};
typedef NS_OPTIONS(NSUInteger, UIViewMarginDirection) {
UIViewMarginDirectionNone = 0,
UIViewMarginDirectionTop = 1 << 0,
UIViewMarginDirectionLeft = 1 << 1,
UIViewMarginDirectionBottom = 1 << 2,
UIViewMarginDirectionRight = 1 << 3,
UIViewMarginDirectionAll = UIViewMarginDirectionTop|UIViewMarginDirectionLeft|UIViewMarginDirectionBottom|UIViewMarginDirectionRight
};
Under MIT license
Copyright (c) 2015 - 2016 neevek <[email protected]>
See the file license.txt for copying permission.