2015-09-07 4 views
0

Я хочу добавить subview с ограничениями программно. Сначала я удалил раскадровку. Но это показывает ошибку. Это мой код. Что проблема? Просьба предоставить мне решение .Whether я должен инициализировать кадр для подвида ..Ограничение для subview программно

APPDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] ; 
[self.window makeKeyAndVisible]; 
self.window.backgroundColor = [UIColor whiteColor]; 
ViewController *view = [[ViewController alloc]init]; 
    UINavigationController *naviOj=[[UINavigationController alloc]initWithRootViewController:view]; 
    self.window.rootViewController=naviOj; 
    [naviOj setNavigationBarHidden:YES animated:YES]; 
return YES; 
} 

ViewController.m

UIView *TopView; 
TopView = [[UIView alloc]init ]; 
TopView.backgroundColor = [UIColor grayColor]; 
[self.view addSubview:TopView]; 
[TopView setTranslatesAutoresizingMaskIntoConstraints:NO]; 

//WidthCons 
    NSLayoutConstraint *WidthCons = [NSLayoutConstraint 
           constraintWithItem:TopView 
           attribute:NSLayoutAttributeWidth 
           relatedBy:NSLayoutRelationEqual 
           toItem:self.view.superview 
           attribute:NSLayoutAttributeWidth 
           multiplier:1 
           constant:0.0]; 
    //HeightCons 
    NSLayoutConstraint *HeightCons = [NSLayoutConstraint 
           constraintWithItem:TopView 
           attribute:NSLayoutAttributeHeight 
           relatedBy:NSLayoutRelationEqual 
           toItem:self.view.superview 
           attribute:NSLayoutAttributeHeight 
           multiplier:.20 
           constant:170]; 

    [self.view addConstraint:WidthCons]; 
    [self.view addConstraint:HeightCons]; 

    //XPosition 
    NSLayoutConstraint *PosX =[ NSLayoutConstraint 
           constraintWithItem:TopView 
           attribute:NSLayoutAttributeCenterX 
           relatedBy:NSLayoutRelationEqual 
           toItem:self.view.superview 
           attribute:NSLayoutAttributeCenterX 
           multiplier:1.0 
           constant:0.0]; 

    //YPosition 
    NSLayoutConstraint *PosY = [NSLayoutConstraint 
           constraintWithItem:TopView 
           attribute:NSLayoutAttributeCenterY 
           relatedBy:NSLayoutRelationEqual 
           toItem:self.view.superview 
           attribute:NSLayoutAttributeCenterY 
           multiplier:1.0 
           constant:0.0]; 

    [self.view addConstraint:PosX]; 
    [self.view addConstraint:PosY]; 

    //Leading 
    NSLayoutConstraint *Leading = [NSLayoutConstraint 
                   constraintWithItem:TopView 
                   attribute:NSLayoutAttributeLeading 
                   relatedBy:NSLayoutRelationEqual 
                   toItem:self.view.superview 
                   attribute:NSLayoutAttributeLeading 
                   multiplier:1.0 
                   constant:0.0]; 

    //Trailing 
    NSLayoutConstraint *Trailing = [NSLayoutConstraint 
            constraintWithItem:TopView 
            attribute:NSLayoutAttributeTrailing 
            relatedBy:NSLayoutRelationEqual 
            toItem:self.view.superview 
            attribute:NSLayoutAttributeTrailing 
            multiplier:1.0 
            constant:0.0]; 

    [self.view addConstraint:Leading]; 
    [self.view addConstraint:Trailing]; 
+4

Не глядя на свой код, сначала сообщите нам, какую ошибку вы получите. –

ответ

0

You не прилагается к self.view.superview, вы должны прикрепить к self.view

self.view - вид самоконтроля. self.view.superview - это представление родительского представления self.view. Вы пытаетесь установить ограничение родительского представления представления текущего вида viewController.

+0

Предоставьте мне пример. Я получаю смущен. Спасибо в Advance !! –

Смежные вопросы