UIStackView的使用
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, getter=isHiddenStatusBar) BOOL hiddenStatusBar;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.hiddenStatusBar = false;
UITapGestureRecognizer *pan = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
pan.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:pan];
[self createView];
}
- (void)createView{
CGFloat height = [UIScreen mainScreen].bounds.size.height;
CGFloat width = [UIScreen mainScreen].bounds.size.width;
int row = 36;//行
int col = 6;//列
CGFloat spacing = 1;
CGFloat top = 20;
CGFloat padding = 1;
int value = 0;
if (self.isHiddenStatusBar) {
top = 0;
spacing = 0;
padding = 0;
}
for (UIView *view in self.view.subviews) {
[view removeFromSuperview];
}
UIStackView *stackViewv = [[UIStackView alloc] initWithFrame:CGRectMake(padding, top + padding, width - 2 * padding, height - top - 2 * padding)];
stackViewv.axis = UILayoutConstraintAxisVertical;
stackViewv.distribution = UIStackViewDistributionFillEqually;
stackViewv.spacing = spacing;
stackViewv.alignment = UIStackViewAlignmentFill;
for (int j = 0; j < row; j++){
UIStackView *stackViewh = [[UIStackView alloc] init];
stackViewh.axis = UILayoutConstraintAxisHorizontal;
stackViewh.distribution = UIStackViewDistributionFillEqually;
stackViewh.spacing = spacing;
stackViewh.alignment = UIStackViewAlignmentFill;
for (int i = 0; i < col; i++) {
UIView *view = [[UIView alloc] init];
int r, g, b;
r = value / 6 / 6;
g = value / 6 % 6;
b = value % 6;
r *= 0x33;
g *= 0x33;
b *= 0x33;
view.backgroundColor = [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1];;
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(0, 0, (width - padding * 2 - spacing *(col - 1)) / col , (height - top - padding * 2 - (row - 1) * spacing) / row);
label.text = [NSString stringWithFormat:@"%02X%02X%02X", r, g, b];
label.textColor = (value / 18) % 2 == 0?[UIColor whiteColor]:[UIColor blackColor];
label.textAlignment = NSTextAlignmentCenter;
label.font = self.isHiddenStatusBar?[UIFont systemFontOfSize:14]:[UIFont systemFontOfSize:12];
[view addSubview:label];
[stackViewh addArrangedSubview:view];
value++;
}
[stackViewv addArrangedSubview:stackViewh];
}
[self.view addSubview:stackViewv];
}
- (void)tapped: (UIGestureRecognizer *)gecognizer {
self.hiddenStatusBar = !self.isHiddenStatusBar;
[self setNeedsStatusBarAppearanceUpdate];
[self createView];
}
- (BOOL)prefersStatusBarHidden {
return self.isHiddenStatusBar;
}
@end
隐藏状态栏,设置 View controller-based status bar appearance
为YES。
网友评论