Created
March 19, 2020 18:58
-
-
Save wozuo/0ada5c5cd12aa24832f01b7191502aa4 to your computer and use it in GitHub Desktop.
How to place UIStackView in UIScrollView
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
// | |
// HomeViewController.m | |
// MYPROJECT | |
// | |
// Created by Marc Tarnutzer on 18.03.20. | |
// Copyright © 2020 Marc Tarnutzer. All rights reserved. | |
// | |
#import "HomeViewController.h" | |
@interface HomeViewController () | |
@end | |
@implementation HomeViewController { | |
UIScrollView *scrollView; | |
UISearchController *searchController; | |
UIStackView *stackView; | |
UIView *testView; | |
} | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
// Do any additional setup after loading the view. | |
printf("HomeViewController didLoad\n"); | |
self.navigationItem.title = @"Home"; | |
// Scroll view setup | |
scrollView = [[UIScrollView alloc] init]; | |
scrollView.alwaysBounceVertical = YES; | |
scrollView.backgroundColor = [UIColor systemBackgroundColor]; | |
scrollView.translatesAutoresizingMaskIntoConstraints = false; | |
[self.view addSubview:scrollView]; | |
[scrollView.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor constant:0].active = YES; | |
[scrollView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:0].active = YES; | |
[scrollView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:0].active = YES; | |
[scrollView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor constant:0].active = YES; | |
// Search controller setup | |
searchController = [[UISearchController alloc] init]; | |
searchController.searchBar.placeholder = @"Search"; | |
self.navigationItem.hidesSearchBarWhenScrolling = YES; | |
self.navigationItem.searchController = searchController; | |
// Stackview | |
stackView = [[UIStackView alloc] init]; | |
stackView.backgroundColor = [UIColor greenColor]; | |
[stackView setAxis:UILayoutConstraintAxisVertical]; | |
[stackView setAlignment:UIStackViewAlignmentFill]; | |
[stackView setDistribution:UIStackViewDistributionEqualSpacing]; | |
[stackView setSpacing:0]; | |
stackView.translatesAutoresizingMaskIntoConstraints = false; | |
[scrollView addSubview:stackView]; | |
[stackView.topAnchor constraintEqualToAnchor:scrollView.topAnchor constant:0].active = YES; | |
[stackView.leadingAnchor constraintEqualToAnchor:scrollView.leadingAnchor constant:0].active = YES; | |
[stackView.trailingAnchor constraintEqualToAnchor:scrollView.trailingAnchor constant:0].active = YES; | |
[stackView.bottomAnchor constraintEqualToAnchor:scrollView.bottomAnchor constant:0].active = YES; | |
[stackView.widthAnchor constraintEqualToAnchor:scrollView.widthAnchor multiplier:1].active = YES; | |
[stackView.widthAnchor constraintEqualToAnchor:self.view.widthAnchor constant:0].active = YES; | |
// Test view inside of stackview | |
testView = [[UIView alloc] init]; | |
testView.backgroundColor = [UIColor systemBackgroundColor]; | |
[stackView addArrangedSubview:testView]; | |
[testView.heightAnchor constraintEqualToConstant:200].active = YES; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment