2013-08-16 24 views
0

我有一個UIView,離屏400像素的左邊,是280像素寬。如何在代碼中將兩個UIViews 270px向右移動到AutoLayout中?

我想主要的UIView向右移動280個像素,並有屏幕外的UIView屏幕同時向右移動,就像一個側邊欄。

我一直在這裏卡住。

有沒有辦法在代碼和使用NSLayoutConstraints做到這一點?

編輯:

-(void) setUpSidebar{ 
    self.sidebarViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"SidebarVC"]; 
    self.sidebarViewController.view.frame = CGRectMake(-400, 0, 280, self.sidebarViewController.view.bounds.size.height); 
    [self.navigationController addChildViewController:self.sidebarViewController]; 
    [self.navigationController.view addSubview:self.sidebarViewController.view]; 
    [self.view insertSubview:self.sidebarViewController.view aboveSubview:self.view]; 
} 

-(void)slideToTheRight{ 
    [UIView animateWithDuration:0.5 animations:^{ 
    self.sidebarOpened = YES; 
    //self.sidebarViewController.view.frame = CGRectMake(-280, 0, 280, self.sidebarViewController.view.bounds.size.height); 
    self.view.frame = CGRectMake(280, 0, self.view.bounds.size.width, self.view.bounds.size.height); 



    }]; 

    [UIView animateWithDuration:0.1 animations:^{ 
    self.sidebarViewController.view.transform = CGAffineTransformMakeTranslation(120, 0); 
    }]; 
} 

-(void) slideBack{ 
    [UIView animateWithDuration:0.3 animations:^{ 
    self.sidebarOpened = NO; 
    //self.sidebarViewController.view.frame = CGRectMake(-280, 0, 280, self.sidebarViewController.view.bounds.size.height); 
    self.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height); 
    }]; 

    [UIView animateWithDuration:0.2 animations:^{ 
    self.sidebarViewController.view.transform = CGAffineTransformMakeTranslation(-120, 0); 
    }]; 
} 

回答

0

當然,你可以做到這一點。使IBOutlet成爲您想要更改的約束,並在代碼中修改其常量參數。所以,如果你有一個對superview左邊緣的約束叫做leftCon,你可以這樣做:

self.leftCon.constant += 200; 
+0

你會如何做一個約束到沒有可見的超級視圖? –

相關問題