Last active
April 21, 2017 02:21
-
-
Save willard1218/04b4aa452ced0d1ad8252cb367152d77 to your computer and use it in GitHub Desktop.
Scrollable stackView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.m | |
// stackVIewtest | |
// | |
// | |
#import "ViewController.h" | |
@interface ViewController () | |
@property UILabel *titleLabel; | |
@property UIScrollView *scrollView; | |
@property UIStackView *stackView; | |
@end | |
@implementation ViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
_titleLabel = [[UILabel alloc] init]; | |
_scrollView = [[UIScrollView alloc] init]; | |
_stackView = [[UIStackView alloc] init]; | |
_titleLabel.translatesAutoresizingMaskIntoConstraints = NO; | |
_titleLabel.text = @"Note"; | |
_scrollView.translatesAutoresizingMaskIntoConstraints = NO; | |
_scrollView.contentSize = CGSizeMake(self.view.frame.size.width, 1500); | |
_scrollView.backgroundColor = [UIColor grayColor]; | |
_stackView.translatesAutoresizingMaskIntoConstraints = NO; | |
_stackView.axis = UILayoutConstraintAxisVertical; | |
_stackView.distribution = UIStackViewDistributionEqualSpacing; | |
_stackView.alignment = UIStackViewAlignmentCenter; | |
_stackView.spacing = 30; | |
[self.view addSubview:_titleLabel]; | |
[self.view addSubview:_scrollView]; | |
[_scrollView addSubview:_stackView]; | |
[self setupLabelConstraint]; | |
[self setupScrollViewConstraint]; | |
[self setupStackViewConstraint]; | |
[self setupViews]; | |
} | |
- (void) viewDidLayoutSubviews { | |
NSLog(@"viewDidLayoutSubviews\n%f : %f",_scrollView.contentSize.height,_stackView.frame.size.height); | |
// _scrollView.contentSize = _stackView.frame.size; | |
} | |
- (void) viewWillAppear:(BOOL)animated { | |
NSLog(@"viewWillAppear\n%f : %f",_scrollView.contentSize.height,_stackView.frame.size.height); | |
// _scrollView.contentSize = _stackView.frame.size; | |
} | |
- (void) viewDidAppear:(BOOL)animated { | |
NSLog(@"viewDidAppear\n%f : %f",_scrollView.contentSize.height,_stackView.frame.size.height); | |
_scrollView.contentSize = _stackView.frame.size; | |
} | |
- (void)setupLabelConstraint { | |
[_titleLabel.topAnchor constraintEqualToAnchor:self.view.topAnchor constant:10].active = YES; | |
[_titleLabel.leftAnchor constraintEqualToAnchor:self.view.leftAnchor constant:10].active = YES; | |
[_titleLabel.widthAnchor constraintEqualToConstant:100].active = YES; | |
[_titleLabel.heightAnchor constraintEqualToConstant:20].active = YES; | |
} | |
- (void)setupScrollViewConstraint { | |
[_scrollView.topAnchor constraintEqualToAnchor:_titleLabel.bottomAnchor constant:10].active = YES; | |
[_scrollView.leftAnchor constraintEqualToAnchor:self.view.leftAnchor constant:10].active = YES; | |
[_scrollView.rightAnchor constraintEqualToAnchor:self.view.rightAnchor constant:-10].active = YES; | |
[_scrollView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor constant:-10].active = YES; | |
//[_scrollView.heightAnchor constraintEqualToConstant:200].active = YES; | |
} | |
- (void)setupStackViewConstraint { | |
[_stackView.leftAnchor constraintEqualToAnchor:_scrollView.leftAnchor].active = YES; | |
[_stackView.rightAnchor constraintEqualToAnchor:_scrollView.rightAnchor].active = YES; | |
[_stackView.topAnchor constraintEqualToAnchor:_scrollView.topAnchor].active = YES; | |
// [_stackView.bottomAnchor constraintEqualToAnchor:_scrollView.bottomAnchor].active = YES; | |
[_stackView.widthAnchor constraintEqualToAnchor:_scrollView.widthAnchor].active = YES; | |
// [_stackView.heightAnchor constraintEqualToAnchor:_scrollView.heightAnchor].active = YES; | |
} | |
- (void)setupViews { | |
for (int i = 0 ; i < 10; i++) { | |
UIView *view1 = [[UIView alloc] init]; | |
view1.backgroundColor = [UIColor blueColor]; | |
view1.translatesAutoresizingMaskIntoConstraints = NO; | |
[view1.heightAnchor constraintEqualToConstant:100].active = YES; | |
[view1.widthAnchor constraintEqualToConstant:120].active = YES; | |
[_stackView addArrangedSubview:view1]; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment