Ich präsentiere einen Viewcontroller wie ein Popup von meinem mainviewcontroller auf iPad, die gut funktioniert. Aber momentan setze ich die bevorzugteContentSize auf den Bildschirm, so dass die Größe beim Drehen nicht autorisiert ist. Ich brauche die Popupansicht, um mit dem Mainviewcontroller mit Breite 97% und Höhe 90% im Hoch- und Querformat ausgerichtet zu sein. Also versuche ich jetzt, die automatische Layout-Beschränkung auf meinem popupViewController hinzuzufügen, bin mir aber nicht sicher, wie. Unten ist der Code, den ich bisher versucht habe. Das gibt mir eine 'NSInternalInconsistencyException' FehlerAutomatisches Hinzufügen von Einschränkungen automatisch bei der Präsentation ViewController
#import "ViewController.h"
@interface ViewController()
{
PopUpViewController *popUpController;
}
@end
- (IBAction)showPopUp:(UIButton *)sender {
popUpController = [[PopUpViewController alloc]initWithNibName:@"PopUpViewController" bundle:nil];
popUpController.view.center = self.view.center;
popUpController.view.layer.cornerRadius = 8.0f;
popUpController.view.layer.masksToBounds = YES;
popUpController.view.layer.borderWidth = 1.0f;
popUpController.view.layer.borderColor = [UIColor blueColor].CGColor;
popUpController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
popUpController.modalPresentationStyle = UIModalPresentationFormSheet;
// CGPoint frameSize = CGPointMake([[UIScreen mainScreen] bounds].size.width*0.97f, [[UIScreen mainScreen] bounds].size.height*0.9f);
// popUpController.preferredContentSize = CGSizeMake(frameSize.x, frameSize.y);
[self addConstraint];
[self.navigationController presentViewController:popUpController animated:YES completion:nil];
}
-(void)addConstraint
{
// Width constraint, half of parent view width
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:popUpController.view.superview
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeWidth
multiplier:0.97
constant:0]];
// Height constraint, half of parent view height
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:popUpController.view.superview
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeHeight
multiplier:0.9
constant:0]];
// Center horizontally
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:popUpController.view.superview
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
// Center vertically
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:popUpController.view.superview
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];
}
Warum fügen Sie Einschränkungen zu self.view hinzu, da es die Referenz auf Unteransichten ist. Sie müssen keine Add-Constraint hinzufügen. Wenn Sie eine Ansicht als Subview von self.view hinzufügen, müssen Sie Constraints hinzufügen. –
Die Höhe und Breite für den 2. View-Controller, den ich präsentiere, wird nicht skaliert. Was soll ich tun, um die Größe zu ändern? Eine Möglichkeit besteht darin, die Größe für ViewWillTransition hinzuzufügen, sieht aber beim Animieren nicht hübsch aus. – Gamerlegend