Skip to content

Instantly share code, notes, and snippets.

@skoch
Created June 12, 2015 16:59
Show Gist options
  • Save skoch/659d906d1808604d0a4e to your computer and use it in GitHub Desktop.
Save skoch/659d906d1808604d0a4e to your computer and use it in GitHub Desktop.
All this to center content in a view for iPhones
- ( void )setContentGutterForIphoneUsingView:( UIView * )view
{
// macro to determine device
if( IS_IPHONE )
{
UIView *container = [UIView new];
container.backgroundColor = [UIColor clearColor];
container.translatesAutoresizingMaskIntoConstraints = NO;
UIView *content = [UIView new];
content.backgroundColor = [UIColor orangeColor];
content.translatesAutoresizingMaskIntoConstraints = NO;
[container addSubview:_contentScrollView];
[container addSubview:content];
[self.view addSubview:container];
// set width and height of the container
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:container
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:self.view.frame.size.width]];
// the view passed in is the menu and this varies based on logged in/out
float gutter = view.frame.size.height + view.frame.origin.y;
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:container
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:self.view.frame.size.height - gutter]];
// put the container at the bottom of the view passed in
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:container
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:view
attribute:NSLayoutAttributeBottom
multiplier:1
constant:0]];
// set the width and height of the content to be whatever the content asset sizes are
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:content
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:kTileSize_iPhone]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:content
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:kTileSize_iPhone]];
// set the content to the midpoint (Y) of the container
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:content
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:container
attribute:NSLayoutAttributeCenterY
multiplier:1
constant:0]];
// layout, so we have our frame for content
[self.view layoutIfNeeded];
// NSLog(@"%@", NSStringFromCGRect(content.frame));
// store for later use
iPhonetileVerticalOffset = content.frame.origin.y;
// and remove this useless view
[content removeFromSuperview];
}
}
@skoch
Copy link
Author

skoch commented Jun 12, 2015

Setting the language to Obj-C does nothing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment