我创建了一个UIview
作为子视图,我想从中心显示和隐藏此视图,问题是帧更改在autolayout中不起作用,这是我的代码,但我想在autolayout中做,我知道nslayout常量以编程方式更改请给我解决方案。 -
(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 :(得分:0)
将pop_view放在视图的中心,方法是在容器中水平放置,在容器约束中垂直放置。
然后为pop_view提供尾随和前导约束和高度。
在(IBAction)click_button1:(id)sender
中,您可以根据需要隐藏或取消隐藏视图。
答案 1 :(得分: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中,您可以执行隐藏/显示操作