以编程方式对子视图进行约束

时间:2015-09-07 08:36:32

标签: ios objective-c

我想以编程方式添加带有约束的子视图。最初我删除了故事板。但它显示错误。这是我的代码。问题是什么?请给我解决方案。是否我必须初始化该子视图的框架..

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];

1 个答案:

答案 0 :(得分:0)

您不需要附加到self.view.superview,您必须附加到self.view

self.view是自我viewController的视图。 self.view.superview是self.view的父视图的视图。您尝试设置当前viewController视图的视图的父视图的约束。