2017-06-14 46 views
0

中心自動佈局POPUP我已創建了一個UIview爲子視圖我要顯示和隱藏中心這一觀點,問題是框架的變化是不是在自動佈局工作,這裏是我的代碼,但我想在自動佈局中做我知道nslayout不斷變化編程請給我解決方案。 -IOS的​​UIView開放作爲但從

(IBAction)click_button1:(id)sender 
{ 
    CGRect screenRect = [[UIScreen mainScreen] bounds]; 
    pop_view.alpha = 0; 
    pop_view.frame = CGRectMake (screenRect.size.width/2, screenRect.size.height/2, 0, 0); 

    pop_view.hidden = false; 

    [UIView animateWithDuration:1.0 animations:^ { 
     pop_view.alpha = 1.0; 

     // pop_view.frame = CGRectMake (screenRect.size.width/2, screenRect.size.height/2, screenRect.size.width - 20, screenRect.size.height/2); 


    pop_view.frame = CGRectMake 
     (
     (self.view.frame.size.width/(CGFloat)2) - (pop_view.frame.size.width/(CGFloat)2), 
     (self.view.frame.size.height/(CGFloat)2) - (pop_view.frame.size.height/(CGFloat)2), 
     pop_view.frame.size.width, 
     pop_view.frame.size.height 
     ); 
    }]; 


} 
+0

您可以將您認爲,爲了實現這一目標。我在當前的項目中做同樣的事情,但它在Swift中,如果你願意,我可以共享代碼。 – Aakash

+0

@Nishant您可以爲想要以編程方式覆蓋的約束創建一個IBOutlet。 IBOutlet var yourConstraint:NSLayoutConstraint! EX: - yourConstraint.constant = 100 – Developer

+0

所以基本上你想要做的是把視圖居中對嗎? – Joshua

回答

0

將pop_view放置在視圖的中心,通過在容器中水平放置並在容器約束中垂直放置。

enter image description here

然後給拖尾和先行約束和高度的pop_view。 在(IBAction)click_button1:(id)sender您可以根據您的要求隱藏或取消隱藏視圖。

0

看來您已經在當前視圖中添加pop_view,只是隱藏它

如果只想居中pop_view

編程這將是這樣的:

.... other declaration ... 
pop_view = popView(); // how ever you instantiate it 
pop_view.translatesAutoresizingMaskIntoConstraints = NO; 
[self.view addSubview:pop_view]; // or whatever your current view is 

// add the constraint to center it horizontally 
[self.view addConstraint:[NSLayoutConstraint constraintWithItem: pop_view 
                 attribute: NSLayoutAttributeCenterX 
                 relatedBy: NSLayoutRelationEqual 
                  toItem: self.view 
                 attribute: NSLayoutAttributeCenterX 
                 multiplier: 1 
                 constant: 0]]; 

// Add the constraint to center it vertically 
[self.view addConstraint:[NSLayoutConstraint constraintWithItem: pop_view 
                 attribute: NSLayoutAttributeCenterY 
                 relatedBy: NSLayoutRelationEqual 
                  toItem: self.view 
                 attribute: NSLayoutAttributeCenterY 
                 multiplier: 1 
                 constant: 0]]; 

然後在你的IBAction爲你只需進行隱藏/顯示動作