Can't remove/hide recently added view from self.navigationcontroller.view - ios

I have a Navigationviewcontroller with a tableview in it. I am adding a UIView to self.navigationcontroller.view and constraints for Auto Layout. When I try to remove it from superview or to hide it, nothing happens.
Any ideas?
How I create the View and Constraints
-(void)doneWithTraining{
//Create Backgroundview
UIView *groupView = [[UIView alloc] init];
[groupView setTranslatesAutoresizingMaskIntoConstraints:NO];
groupView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
groupView.tag = 999;
groupView.alpha = 0.0;
[self.navigationController.view addSubview:groupView];
[self.navigationController.view addConstraint:[NSLayoutConstraint constraintWithItem:groupView
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:self.navigationController.view
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:0]];
[self.navigationController.view addConstraint:[NSLayoutConstraint constraintWithItem:groupView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.navigationController.view
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0f]];
[self.navigationController.view addConstraint:[NSLayoutConstraint constraintWithItem:groupView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.navigationController.view
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0.0]];
[self.navigationController.view addConstraint:[NSLayoutConstraint constraintWithItem:groupView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.navigationController.view
attribute:NSLayoutAttributeHeight
}
multiplier:1.0
constant:0.0]];
//Animate View
[UIView animateWithDuration:0.2 animations:^(void) {
groupView.alpha = 1.0;
}];
How I try to remove the View
- (void)closeFinishAlert{
UIView *grouView = [self.navigationController.view viewWithTag:999];
grouView.hidden = YES;
NSLog(#"Check if closeFinishAlert is called");
//[self.navigationController popViewControllerAnimated:YES];
}
What I also tried
This removes the view an its content, but after this there is no user interaction possible. It looks like a crash, but Xcode tells me that the app is still running.
- (void)closeFinishAlert{
UIView *groupView = [self.navigationController.view viewWithTag:999];
for (UIView *subview in groupView.subviews) {
subview.hidden = YES;
[subview removeFromSuperview];
}
[_groupView removeFromSuperview];
//[self.navigationController popViewControllerAnimated:YES];
}

If you are actually just presenting a view which you will remove for sure later on,
then its better to create a ViewController class for your view and present it modally using :
[self presentViewController:yourViewControllerObject animated:NO completion:nil];
you can later on remove this view using:
[self dismissViewControllerAnimated:NO completion:nil];
hope this helps !

Thanks to "GoGreen" green I found a solution which works for me.
Create Viewcontroller from ViewDidAppear
- (void)viewDidAppear:(BOOL)animated{
if (executedExercises.count == [_fetchedResultsController.fetchedObjects count] && self.groupView == FALSE) {
[self performSelector:#selector(doneWithTraining) withObject:nil afterDelay:0.0];
}
}
Header file
#property (strong, nonatomic) UIViewController *groupView;
Creating the Viewcrontroller and its content
-(void)doneWithTraining{
_groupView = [[UIViewController alloc] init];
[self presentViewController:_groupView animated:YES completion:nil];
_groupView.view.backgroundColor = [UIColor whiteColor];
//SubTitle
UILabel *subTitleLabel = [[UILabel alloc] init];
[subTitleLabel setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:18.0]];
subTitleLabel.textColor = [BackgroundLayer gray];
[subTitleLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
subTitleLabel.numberOfLines = 5;
subTitleLabel.adjustsFontSizeToFitWidth = YES;
subTitleLabel.textAlignment = NSTextAlignmentCenter;
[_groupView.view addSubview:subTitleLabel];
subTitleLabel.text = subTitleText;
[_groupView.view addConstraint:[NSLayoutConstraint constraintWithItem:subTitleLabel
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationLessThanOrEqual
toItem:_groupView.view
attribute:NSLayoutAttributeWidth
multiplier:0.0
constant:300.0f]];
[_groupView.view addConstraint:[NSLayoutConstraint constraintWithItem:subTitleLabel
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:_groupView.view
attribute:NSLayoutAttributeHeight
multiplier:0.0
constant:140]];
[_groupView.view addConstraint:[NSLayoutConstraint constraintWithItem:subTitleLabel
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:_groupView.view
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
[_groupView.view addConstraint:[NSLayoutConstraint constraintWithItem:subTitleLabel
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:_groupView.view
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];}
Removing ViewController
- (void)closeFinishAlert{
[_groupView dismissViewControllerAnimated:NO completion:nil];
[self.navigationController popViewControllerAnimated:YES];
}

Related

Why isn't the camera working on my iPad with MTBBarcodeScanner?

I've written a simple UIViewController that uses MTBBarcodeScanner to scan barcodes. It works perfectly on my iPhone 6s+ running iOS 10, no matter if it's a development build from Xcode or an AdHoc distribution from diawi.com. Unfortunately, it's not working on iPad (also running iOS 10), when distributed through Xcode or the AdHoc thing. My view controller slides in correctly, the buttons work as expected, but the preview window shows no video and no barcodes ever get read.
Before this view controller loads, I've already checked for permission with the user. I got the popup from iOS and granted it on both platforms. The Settings app for the iPad shows that I indeed have access to the camera.
Here's my code.
#import "BarCodeScannerViewController.h"
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
#import <MTBBarcodeScanner/MTBBarcodeScanner.h>
#interface BarCodeScannerViewController ()
#property (nonatomic, strong) MTBBarcodeScanner *scanner;
#end
#implementation BarCodeScannerViewController
- (void) loadView; {
[super loadView];
[self setTitle:#"Scan Bar Code"];
UIView *preview = [[UIView alloc] init];
[[self view] addSubview:preview];
[preview setTranslatesAutoresizingMaskIntoConstraints:NO];
[self setScanner:[[MTBBarcodeScanner alloc] initWithMetadataObjectTypes:#[AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeQRCode] previewView:preview]];
UIView *buttonBar = [[UIView alloc] init];
[[self view] addSubview:buttonBar];
[buttonBar setTranslatesAutoresizingMaskIntoConstraints:NO];
[buttonBar setBackgroundColor:[UIColor colorWithWhite:0 alpha:.5]];
UIButton *cancelButton = [[UIButton alloc] init];
[buttonBar addSubview:cancelButton];
[cancelButton setTranslatesAutoresizingMaskIntoConstraints:NO];
[cancelButton addTarget:self action:#selector(cancel) forControlEvents:UIControlEventTouchUpInside];
[cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[cancelButton setTitle:#"Cancel" forState:UIControlStateNormal];
UIButton *torchToggle = nil;
if ([[self scanner] hasTorch]) {
UIButton *torchToggle = [[UIButton alloc] init];
[buttonBar addSubview:torchToggle];
[torchToggle setTranslatesAutoresizingMaskIntoConstraints:NO];
[torchToggle addTarget:[self scanner] action:#selector(toggleTorch) forControlEvents:UIControlEventTouchUpInside];
[torchToggle setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[torchToggle setTitle:#"Toggle Light" forState:UIControlStateNormal];
}
[[self view] addConstraint:[NSLayoutConstraint constraintWithItem:preview attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual
toItem:[self view] attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
[[self view] addConstraint:[NSLayoutConstraint constraintWithItem:preview attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual
toItem:[self view] attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0]];
[[self view] addConstraint:[NSLayoutConstraint constraintWithItem:preview attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual
toItem:[self view] attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0]];
[[self view] addConstraint:[NSLayoutConstraint constraintWithItem:preview attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual
toItem:[self view] attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
[[self view] addConstraint:[NSLayoutConstraint constraintWithItem:buttonBar attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual
toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:44.0]];
[[self view] addConstraint:[NSLayoutConstraint constraintWithItem:buttonBar attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual
toItem:[self view] attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0]];
[[self view] addConstraint:[NSLayoutConstraint constraintWithItem:buttonBar attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual
toItem:[self view] attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0]];
[[self view] addConstraint:[NSLayoutConstraint constraintWithItem:buttonBar attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual
toItem:[self view] attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
[buttonBar addConstraint:[NSLayoutConstraint constraintWithItem:cancelButton attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual
toItem:buttonBar attribute:NSLayoutAttributeLeft multiplier:1.0 constant:5.0]];
[buttonBar addConstraint:[NSLayoutConstraint constraintWithItem:cancelButton attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual
toItem:buttonBar attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-5.0]];
if ([[self scanner] hasTorch]) {
[buttonBar addConstraint:[NSLayoutConstraint constraintWithItem:torchToggle attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual
toItem:buttonBar attribute:NSLayoutAttributeRight multiplier:1.0 constant:-5.0]];
[buttonBar addConstraint:[NSLayoutConstraint constraintWithItem:torchToggle attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual
toItem:buttonBar attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-5.0]];
}
[_scanner startScanningWithResultBlock:^(NSArray *codes) {
for (AVMetadataMachineReadableCodeObject *code in codes) {
if ([code stringValue]) {
[_scanner stopScanning];
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
if ([self barCodeScanned]) [self barCodeScanned]([code stringValue], self);
break;
}
}
} error:nil];
}
- (void) cancel;
{
[[self scanner] stopScanning];
[self dismissViewControllerAnimated:YES completion:nil];
}
#end
Thanks to Mike Buss for assisting me with this. He told me that I was probably starting the camera too early. He suggested I move the startScanningWithResultBlock: call to viewDidAppear:animated. I tested it with my iPad and it's working great now.

changes subView frame when rotate

i add subView(backgroundView) in uiview in my base view controller its working well in potratate but when i change orientation from potraite to landscape its frame size is same as potrait i want to change size of subview when rotate.
UIView *activityView = [[UIView alloc] initWithFrame:self.view.bounds];
CGRect frame = activityView.frame;
activityView.frame = frame;
activityView.backgroundColor = [UIColor clearColor];
activityView.alpha = 0.0f;
[self.view addSubview:activityView];
self.activityView = activityView;
UIView *backgroundView = [[UIView alloc]initWithFrame:activityView.bounds];
backgroundView.alpha = 0.0f;
[backgroundView setBackgroundColor:[UIColor lightGrayColor]];
[self.activityView backgroundView];
UIActivityIndicatorView *spinning = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[self.activityView spinning];
spinning.center = activityView.center;
self.activityView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
[spinning setColor:[UIColor lightGrayColor]];
[spinning startAnimating];
To do this you must add constraints to your view (AutoLayout).
For example :
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:containerView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:containerView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:containerView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:containerView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:0.0]];
Edit :
Don't forget to set translatesAutoresizingMaskIntoConstraints property to NO on views.
(Replace self.view and containerView with your views).

iOS - navigation bar displayed when UISearchController is active

I programatically created a UISearchController and added it to my UIView A. I also have a UITableView in the same UIView. I use UISearchController to search through my UITableView. When you click on a cell, UIView B gets pushed. Both UIView A and UIView B have UINavigationBar set to HIDDEN.
When I simply click on a UITableViewCell WITHOUT searching, everything happens perfectly, and UIView B is displayed without UINavigationBar.
But when I am searching using the UISearchController and I click on a UITableViewCell, the UINavigationBar is displayed on UIView B, even though I set it to hidden in the viewDidAppear method.
Here is my code:
BarsSearchController = [[UISearchController alloc] initWithSearchResultsController:nil];
BarsSearchController.searchResultsUpdater = self;
BarsSearchController.dimsBackgroundDuringPresentation = NO;
BarsSearchController.searchBar.delegate = self;
BarsSearchController.searchBar.barTintColor = [UIColor whiteColor];
BarsSearchController.searchBar.searchBarStyle = UISearchBarStyleMinimal;
[searchBarView addSubview:BarsSearchController.searchBar];
[searchBarView addConstraint:[NSLayoutConstraint constraintWithItem:BarsSearchController.searchBar
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:searchBarView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0]];
[searchBarView addConstraint:[NSLayoutConstraint constraintWithItem:BarsSearchController.searchBar
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:searchBarView
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0.0]];
[searchBarView addConstraint:[NSLayoutConstraint constraintWithItem:BarsSearchController.searchBar
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:searchBarView
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0.0]];
[searchBarView addConstraint:[NSLayoutConstraint constraintWithItem:BarsSearchController.searchBar
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:searchBarView
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:0.0]];
[searchBarView layoutIfNeeded];
self.definesPresentationContext = YES;
[BarsSearchController.searchBar sizeToFit];
What am I missing here?
BarsSearchController.hidesNavigationBarDuringPresentation = YES;
I have the same problem, to fix it, I hide back button and background in viewDidLoad :
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.hidesBackButton = YES;
[[[self navigationController] navigationBar] setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[[[self navigationController] navigationBar] setShadowImage:[UIImage new]];
[[[self navigationController] navigationBar] setTranslucent:YES];
}
and I hide navigationBar in viewDidAppear :
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[[self navigationController] setNavigationBarHidden:YES];
}

How to add custom view with autolayout programmatically

My storyboard has a single view controller containing a UIScrollView. I want to add some custom view (each view will contain a image & label) to the scrollview programmatically in the viewController. I have set constraints to the UIScrollView in the storyboard but struggling to add custom views with constraints. How can i do this so that it can be appeared as same for all the devices (iPhone/iPad) ? I tried like below but it does not seems to be working.
In My ViewController.m -
#import "MainViewController.h"
#define DEFAULT_ROW_HEIGHT 50
#interface MainViewController ()
#property float topMarginFromUpperView;
#end
#implementation MainViewController
-(void) addCustomRowToView {
self.topMarginFromUpperView += DEFAULT_ROW_HEIGHT + 10.0f;
UIView *customRow = [[UIView alloc] initWithFrame:self.scrollView.bounds];
customRow.backgroundColor = [UIColor redColor];
[self.scrollView addSubview:customRow];
customRow.translatesAutoresizingMaskIntoConstraints = NO;
[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:customRow attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeTop multiplier:1.0f constant:self.topMarginFromUpperView]];
[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:customRow attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:10.0f]];
[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:customRow attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-10.0f]];
[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:customRow attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeHeight multiplier:0.1f constant:0.0f]];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.topMarginFromUpperView = 0.0f;
for (NSInteger i =0; i< 10; i++) {
[self addCustomRowToView];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
EDITED :
Still struggling with constraint setting. App crashes due to invalid constraint. Tried as below -
-(void) setConstraints:(UIView *)customRow {
[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:customRow attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeTop multiplier:1.0f constant:self.topMarginFromSuperView]];
[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:customRow attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:10.0f]];
[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:customRow attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-10.0f]];
[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:customRow attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeHeight multiplier:0.1f constant:0.0f]];
}
-(void) addCustomRowToView {
UIView *customRow = [[UIView alloc]initWithFrame:CGRectMake(DEFAULT_PADDING, self.topMarginFromSuperView, self.view.bounds.size.width - 2*DEFAULT_PADDING, DEFAULT_ROW_HEIGHT)];
customRow.backgroundColor = [UIColor redColor];
customRow.translatesAutoresizingMaskIntoConstraints = NO;
[self setConstraints:customRow];
[self.scrollView addSubview:customRow];
self.scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, self.topMarginFromSuperView + DEFAULT_ROW_HEIGHT);
self.topMarginFromSuperView += DEFAULT_ROW_HEIGHT + DEFAULT_PADDING;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.topMarginFromSuperView = 0.0f;
//for (NSInteger i =0; i< 10; i++) {
[self addCustomRowToView];
// }
}
#end
Here is solution for your problem:
- (void)addCustomView
{
UIView *lastView;
for (int i = 0; i<10; i++)
{
UIView *view = [[UIView alloc] init];
view.backgroundColor = i%2 ? [UIColor redColor] : [UIColor greenColor];
view.translatesAutoresizingMaskIntoConstraints = NO;
[self.scrollView addSubview:view];
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-10-[view]-10-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)]];
[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeWidth multiplier:1 constant:-20.0]];
if (i == 0)
{
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[view(40)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)]];
}
else
{
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:[lastView(40)]-10-[view(40)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(lastView,view)]];
}
lastView = view;
}
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:[lastView(40)]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(lastView)]];
}

iOS Auto layout Rows

I'm trying to understand auto layout better and I created some static methods that can help me out. Now I'm trying to setup an array of views in like a tableview structure (in rows). This works kinda.
See here my problem:
To achieve this I use this code:
//In the update constraints method
UIView *view1 = [self createView];
view1.backgroundColor = [UIColor redColor];
UIView *view2 = [self createView];
view2.backgroundColor = [UIColor yellowColor];
[self addSubview:view1];
[self addSubview:view2];
[self addConstraints:[DXVFL row:#[view1, view2] inView:self]];
//The row method
+ (NSArray *) row:(NSArray const *)views inView:(UIView const *)superview {
NSMutableArray *constraints = [NSMutableArray new];
CGFloat multiplier = 1.f / [views count];
UIView *prev = nil;
for (UIView *view in views) {
if (!view.hidden) {
[constraints addObject:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeWidth multiplier:1.f constant:0.f]];
[constraints addObject:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeHeight multiplier:multiplier constant:0.f]];
if (prev) {
[constraints addObject:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:prev attribute:NSLayoutAttributeBottom multiplier:1.f constant:0.f]];
}
prev = view;
}
}
return [constraints copy];
}
So this part works (I guess). But when I want to split the yellow view again in rows. The views are always added into the top region?
In the update constraints method I do this:
UIView *view1 = [self createView];
view1.backgroundColor = [UIColor redColor];
UIView *view2 = [self createView];
view2.backgroundColor = [UIColor yellowColor];
[self addSubview:view1];
[self addSubview:view2];
UIView *view3 = [self createView];
UIView *view4 = [self createView];
[view2 addSubview:view3];
[view2 addSubview:view4];
[self addConstraints:[DXVFL row:#[view1, view2] inView:self]];
[view2 addConstraints:[DXVFL row:#[view3, view4] inView:view2]];
But I want those two rows in the bottom? What am I doing wrong?
Off the top of my head:
You need to add a constraint to tell it to align the view to the top of the superview. At the moment, you just set the height and width but you don't tell it where to put its top edge.
Try changing your code to this:
if (!prev) {
[constraints addObject:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeTop multiplier:1.f constant:0.f]];
} else {
[constraints addObject:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:prev attribute:NSLayoutAttributeBottom multiplier:1.f constant:0.f]];
}

Resources