我怎样才能使用Autolay编程在一个现有的UIView的顶部的UIView?

如果Foursquare签入成功,我的iPhone应用程序显示会在显示的视图顶部添加一个视图。

我希望视图是以X和Y为中心,并使用自动布局有一个确定的宽度和高度,但我不知道哪些约束,以编程方式添加。 我知道如何在Storyboard中做到这一点,但是我不确定究竟input什么代码来做同样的事情。

我无法正常工作,UIView有一个用loadNibNamed加载的笔尖:

SuccessView *successfulCheckInView = [[SuccessView alloc] initWithFrame:CGRectZero]; successfulCheckInView.placeNameLabel.text = properVenue.name; successfulCheckInView.placeAddressLabel.text = properVenue.address; successfulCheckInView.delegate = self; [self.ownerVC.view addSubview:successfulCheckInView]; 

尝试这个:

 NSLayoutConstraint *xCenterConstraint = [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]; [superview addConstraint:xCenterConstraint]; NSLayoutConstraint *yCenterConstraint = [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]; [superview addConstraint:yCenterConstraint]; 

更新了Swift:

 NSLayoutConstraint(item: view1, attribute: .centerX, relatedBy: .equal, toItem: view2, attribute: .centerX, multiplier: 1, constant: 0).isActive = true NSLayoutConstraint(item: view1, attribute: .centerY, relatedBy: .equal, toItem: view2, attribute: .centerY, multiplier: 1, constant: 0).isActive = true