[英]animating a view on top of uitableview
I have a UINavigationController
that has my custom ViewController
with a tableView
in it that takes up the whole screen. 我有一个UINavigationController
,其中包含我的自定义ViewController
和一个tableView
,它占据了整个屏幕。
I want to push down the tableView
and reveal a settings menu with just a couple items. 我想按下tableView
并显示仅包含几个项目的设置菜单。 My SettingsView.xib
I created in a separate nib that is 320 x 90 and just has a single UISegmentedControl
in it. 我在一个320 x 90的单独笔尖中创建的SettingsView.xib
,其中只有一个UISegmentedControl
。
I have an Outlet for the NSLayoutConstraint
I created between the tableView
and the top layout guide. 我在tableView
和顶部布局指南之间为NSLayoutConstraint
创建了一个插座。 When my settingsButton
is pressed, I do this 当我按下settingsButton
,我会执行此操作
func showSettings(sender: UIBarButtonItem) {
println(String(format: "%@", NSStringFromCGRect(settingsView.frame)))
println(String(format: "tableview %@", NSStringFromCGRect(tableView.frame)))
isSettingsOpen = !isSettingsOpen
settingsHeightConstraint.constant = isSettingsOpen ? 90.0 : -64.0
UIView.animateWithDuration(1.0, delay: 0.0, usingSpringWithDamping: 0.4, initialSpringVelocity: 8.0, options: .CurveEaseIn, animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
That part slides down as I expect. 这部分滑落了我所期望的。 Then I added the settingsView like so: 然后,我像这样添加了settingsView:
lazy var settingsView: UIView = self.initializeSettingsView()
func initializeSettingsView() -> UIView {
let nib = UINib(nibName: "SettingsView", bundle: nil)
let nibView = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
return nibView;
}
// in viewDidLoad
settingsView.center.y -= self.settingsView.frame.height
view.addSubview(settingsView)
var constraints = [NSLayoutConstraint]()
constraints.append(NSLayoutConstraint(item: settingsView, attribute: NSLayoutAttribute.Bottom, relatedBy: .Equal, toItem: tableView, attribute: .Top, multiplier: 1.0, constant: 0.0)) // this constraint seems to be the problem one
constraints.append(NSLayoutConstraint(item: settingsView, attribute: .Left, relatedBy: .Equal, toItem: tableView, attribute: .Left, multiplier: 1.0, constant: 0.0))
constraints.append(NSLayoutConstraint(item: settingsView, attribute: .Right, relatedBy: .Equal, toItem: tableView, attribute: .Right, multiplier: 1.0, constant: 0.0))
NSLayoutConstraint.activateConstraints(constraints)
Then I get ambiguous layout. 然后我得到模棱两可的布局。 I know that the height constraint that I try to animate and the first constraint that I put a comment on are conflicting with each other because if I comment one of those lines out, I don't get the error message. 我知道我尝试设置动画的高度约束和我发表评论的第一个约束相互冲突,因为如果我注释掉其中的一行,则不会收到错误消息。
What I'm trying to do with that first constraint is just have the settingsView
"pinned" to the edge of the tableView
so that way when I animate the tableView
moving downwards by 90.0 then the settingsView
would go with it. 我要对第一个约束进行的操作只是将settingsView
“固定”到tableView
的边缘,这样,当我对tableView
向下移动90.0进行动画处理时, settingsView
就会随之出现。
This doesn't seem to be the case and I can't figure out why. 似乎并非如此,我不知道为什么。 I haven't really done much with animating views when constraints are involved. 当涉及到约束时,我在动画视图方面并没有做很多事情。 Did I set this up correctly? 我设置正确吗? Do I need to remove one of the constraints and then add a different one? 我是否需要删除其中一个约束,然后添加其他约束? Thanks in advance. 提前致谢。
In initializeSettingsView()
add nibView.setTranslatesAutoresizingMaskIntoConstraints(false)
. 在initializeSettingsView()
添加nibView.setTranslatesAutoresizingMaskIntoConstraints(false)
。 And then in viewDidload
add the hight constraint to settingsView 然后在viewDidload
中将hight约束添加到settingsView
constraints.append(NSLayoutConstraint(item: settingsView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 90.0))
And you do not need to set the position of settingsView
, since it will be layout at the right position based on the constraints you add. 而且您不需要设置settingsView
的位置,因为它将根据您添加的约束在正确的位置进行布局。 So just remove settingsView.center.y -= settingsView.frame.size.height
因此,只需删除settingsView.center.y -= settingsView.frame.size.height
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.