Using AutoLayout with Grids


#1

I am attempting to layout a grid inside another view.  At first approximation, I want it to take up 100% of the superview, but I can’t figure out how to make that happen.

No matter the exact form I try, I get the grid hanging off the upper left of the superview, or missing entirely.

Does this grid control support using AutoLayout, and if so, how?  If it doesn’t support auto layout, how do I place it in an otherwise autolaid out view hierarchy?  Thanks!

[self.gridsetTranslatesAutoresizingMaskIntoConstraints:NO];

[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[grid]" options:0 metrics:nil views:views]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[grid]" options:0 metrics:nil views:views]];

#2

Greetings Program!

I have no useful experience with AutoLayout :slight_smile: but when you create the grid just create it using the bounds of the superview:

// create a grid - with a 40 pixel padding
_shinobiDataGrid = [[ShinobiDataGrid alloc] initWithFrame:CGRectInset(myView.bounds, 40, 40)];

// add to the view
[myView addSubview:_shinobiDataGrid];

Wg


#3

I ended up with a somewhat hacky solution to this. I’d still love to use autolayout, but this works.

In the ViewController that contains the grid, use code that looks something like this:

// At this point, we know the calculated frames, so we can use grid
-(void)viewDidLayoutSubviews
{
  CGRect gridFrame = CGRectInset([self.view bounds], 40, 40);
  self.grid.frame = gridFrame;
}

#4

From your hack, are you adding the grid to a dynamic (sized) view that’s sized/finished sizing after the grid has been added?

Wg


#5

I am new to IOS programming.  But the workflow I have, as I understand it is:

ViewController gets initialized, and creates the grid with CGRectZero as its frame. Immediately adds it as a subview.

Autolayout happens (sometime after viewDidLoad, and before viewDidLayoutSubviews)

At this point, we know the calculated sizes of the frames that are done by autolayout.

So we can ask for what that calculated frame really was, and hand it off to the grid for its own manual layout.

I have zero clue how it’d work in the face of device rotation, and similar changes in sizes, but luckily that’s not relevant for my specific use case.


#6

Good to know; I’ve always added the grid to an existing view with the bounds already set.

One day I will tackle AutoLayout…just not today or anytime soon :slight_smile:

Wg


#7

Hi Cschneid,

Currently, the grid itself doesn’t currently support use with AutoLayout.

I would recommend overriding layout subviews to manually set your grid to the size of your cell:

-(void)layoutSubviews
{
  [super layoutSubviews];
  self.grid.frame = CGRectInset(self.view.bounds, 40, 40);
}

That’s how we implement our cell subclasses in our samples.

I hope the above helps.

Jan


#8

How do you handle device rotation? My grid isnt expanding…or do I have to resize everything on rotation?


#9

Well, I tackled AutoLayout sooner than I expected to; had to deal with it in another project :slight_smile:

This is what I do when rotating:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
	[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
	
	shinobiDataGrid.frame = self.view.bounds;
}

- (NSUInteger)supportedInterfaceOrientations
{
	return UIInterfaceOrientationMaskAll;
}

- (BOOL)shouldAutorotate
{
	return YES;
}

Wg