Skip to content

Commit

Permalink
using topLayoutGuide and bottomLayoutGuide to align
Browse files Browse the repository at this point in the history
  • Loading branch information
yoiang committed Nov 4, 2014
1 parent 39932a6 commit 9d1ee3c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 57 deletions.
3 changes: 2 additions & 1 deletion StoryboardXibController/StoryboardXibController.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
@property (readwrite) IBInspectable NSString *screenNib;
@property (readwrite) IBInspectable NSString *nibBundleName;

@property (readwrite) IBInspectable BOOL alignContentToNavigationBar;
@property (readwrite) IBInspectable BOOL alignToTopLayoutGuide;
@property (readwrite) IBInspectable BOOL alignToBottomLayoutGuide;

@property (strong, readonly) UIViewController *containedController;

Expand Down
95 changes: 41 additions & 54 deletions StoryboardXibController/StoryboardXibController.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ @interface StoryboardXibController ()
@property (strong, readwrite) UIStoryboardSegue *segue;
@property (strong, readwrite) id segueInfo;

@property (weak, readwrite) NSLayoutConstraint *containedViewTopLayoutContraint;

@end

@implementation StoryboardXibController
Expand Down Expand Up @@ -112,20 +110,6 @@ - (void)viewDidLoad
[self createSubViewController];
}

- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];

[self updateContainedViewTopConstraint];
}

- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];

[self updateContainedViewTopConstraint];
}

- (BOOL)checkContainedViewDidLoad
{
if (self.containedController.isViewLoaded)
Expand Down Expand Up @@ -183,15 +167,27 @@ - (void)setupContainedViewConstraints
{
UIView *containedView = self.containedController.view;

NSLayoutConstraint *containedViewTopLayoutContraint = [NSLayoutConstraint constraintWithItem:containedView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTop
multiplier:1.0f
constant:0];
NSLayoutConstraint *containedViewTopLayoutContraint;
if (self.alignToTopLayoutGuide)
{
containedViewTopLayoutContraint = [NSLayoutConstraint constraintWithItem:containedView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.topLayoutGuide
attribute:NSLayoutAttributeBottom
multiplier:1.0f
constant:0];
} else
{
containedViewTopLayoutContraint = [NSLayoutConstraint constraintWithItem:containedView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTop
multiplier:1.0f
constant:0];
}
[self.view addConstraint:containedViewTopLayoutContraint];
self.containedViewTopLayoutContraint = containedViewTopLayoutContraint;

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:containedView
attribute:NSLayoutAttributeLeft
Expand All @@ -201,13 +197,27 @@ - (void)setupContainedViewConstraints
multiplier:1.0f
constant:0.0] ];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:containedView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1.0f
constant:0.0] ];
NSLayoutConstraint *containedViewBottomLayoutContraint;
if (self.alignToBottomLayoutGuide)
{
containedViewBottomLayoutContraint = [NSLayoutConstraint constraintWithItem:containedView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.bottomLayoutGuide
attribute:NSLayoutAttributeTop
multiplier:1.0f
constant:0];
} else
{
containedViewBottomLayoutContraint = [NSLayoutConstraint constraintWithItem:containedView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1.0f
constant:0];
}
[self.view addConstraint:containedViewBottomLayoutContraint];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:containedView
attribute:NSLayoutAttributeRight
Expand All @@ -216,29 +226,6 @@ - (void)setupContainedViewConstraints
attribute:NSLayoutAttributeRight
multiplier:1.0f
constant:0.0] ];

[self updateContainedViewTopConstraint];
}

- (void)updateContainedViewTopConstraint
{
CGFloat offsetTop = 0;
if ( self.alignContentToNavigationBar && [self isWithinNavigationController] )
{
offsetTop = [self navigationBarFrame].origin.y + [self navigationBarFrame].size.height;
}
self.containedViewTopLayoutContraint.constant = offsetTop;
}

- (BOOL)isWithinNavigationController
{
return self.navigationController != nil;
}

- (CGRect)navigationBarFrame
{
return self.navigationController.navigationBar.frame;
//[self.navigationController.navigationBar convertRect:self.navigationController.navigationBar.frame toView:self.view];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="string" keyPath="screenControllerClass" value="XibViewController"/>
<userDefinedRuntimeAttribute type="string" keyPath="screenNib" value="SecondView"/>
<userDefinedRuntimeAttribute type="boolean" keyPath="alignContentToNavigationBar" value="YES"/>
<userDefinedRuntimeAttribute type="boolean" keyPath="alignToTopLayoutGuide" value="YES"/>
<userDefinedRuntimeAttribute type="boolean" keyPath="alignToBottomLayoutGuide" value="YES"/>
</userDefinedRuntimeAttributes>
<connections>
<segue destination="KCG-Ai-Gye" kind="push" identifier="Next" id="a40-TV-CRM"/>
Expand All @@ -91,7 +92,8 @@
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="string" keyPath="screenControllerClass" value="XibViewController"/>
<userDefinedRuntimeAttribute type="string" keyPath="screenNib" value="ThirdView"/>
<userDefinedRuntimeAttribute type="boolean" keyPath="alignContentToNavigationBar" value="YES"/>
<userDefinedRuntimeAttribute type="boolean" keyPath="alignToTopLayoutGuide" value="YES"/>
<userDefinedRuntimeAttribute type="boolean" keyPath="alignToBottomLayoutGuide" value="YES"/>
</userDefinedRuntimeAttributes>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="0nP-RK-EHz" userLabel="First Responder" sceneMemberID="firstResponder"/>
Expand Down

1 comment on commit 9d1ee3c

@yoiang
Copy link
Member Author

@yoiang yoiang commented on 9d1ee3c Nov 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cleaner solution for #2

Please sign in to comment.