Centering Modal View with Autolayout
Could you try this?
Declare your modal window view as subclass of UIView
, and implement didMoveToSuperView
.
- (void)didMoveToSuperview {
UIView *superView = self.superview;
if(superView == nil) {
return;
}
[superView addConstraint:[NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:superView
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0]];
[superView addConstraint:[NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:superView
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0]];
// [superView layoutIfNeeded]; // If this does not work, try uncomment this.
}
This should automatically do centering itself when added to any superview.
Needles to say, you also need translatesAutoresizingMaskIntoConstraints = NO
and any width/height constraints.
I haven't tested with UITransitionView
, though. Maybe this conflict with your positionAnimation
.
EDIT: 2014/09/22
Instead of using Autolayout constraints to modal view itself, I think you should use systemLayoutSizeFittingSize:
method without translatesAutoresizingMaskIntoConstraints = NO
.
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
// ...snip
UIView *toView = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey].view;
toView.translatesAutoresizingMaskIntoConstraints = YES; // To clarify. You don't need this line because this is the default.
CGSize sysSize = [toView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
toView.bounds = CGRectMake(0, 0, sysSize.width, sysSize.height);
toView.center = CGPointMake(transitionContext.containerView.center.x, -transitionContext.containerView.center.y);
toView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin;
// ...snip
}
and If you want resize modal view after display (as a side effect of modifying it's content), do like following code in your modal views UIViewController
subclass:
- (void)yourAppMethod {
NSString *message = @""; // <- as u like
UILabel *label = self.messageLabel;
label.text = message;
[self resizeViewIfNeeded]; // <- this will resize self.view
}
- (void)resizeViewIfNeeded {
CGSize sysSize = [self.view systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
if(!CGSizeEqualToSize(sysSize, self.view.bounds.size)) {
self.view.bounds = CGRectMake(0, 0, sysSize.width, sysSize.height);
}
}
You can programmatically add a centering constraint from containerView to toView in your animateTranisition method:
(in Swift, but you should be able to get the idea...)
containerView.addSubview(toView)
let centerXLayoutConstraint = NSLayoutConstraint(item: toView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: containerView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
let centerYLayoutConstraint = NSLayoutConstraint(item: toView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: containerView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)
containerView.addConstraint(centerXLayoutConstraint)
containerView.addConstraint(centerYLayoutConstraint)
When I tried this, I also added width and height constraints to toView to size it relative to containerView. It worked -- no problem.
I think it should work with a self-sizing toView as well. You might have to override intrinsicSize in your toView class and/or play around with forcing it to update its constraints.