UITableView自上而下布局指南以编程方式插入内容
由于我试图正确实现iAds,即共享ADBannerView的单个实例,因此我正在UIViewController中以编程方式创建UITableView,并将其添加到视图中。下面是我的UIViewController子类的几个片段: 从视图下载 iAds的实现增加了尝试和证明我的UITableView实现的正确性或错误性 如您所见,注释掉了3个约束。每种方法似乎都有不同的结果。除非被要求,否则我不会发布它们的截图,但我会描述它们 NSLayoutConstraintitem:self.tableView,属性:.Top,relatedBy:.Equal,toItem:self.view,属性:.Top,乘数:1,常数:0 iOS 7:表格顶部,表格内容位于屏幕顶部。内容在导航栏后面 iOS 8:表格顶部和表格内容位于导航栏下方。内容在导航栏下方是否正确 NSLayoutConstraintitem:self.tableView,属性:.Top,relatedBy:.Equal,toItem:self.view,属性:.Top,乘数:1,常数:self.topLayoutGuide.length iOS 7:表格顶部,表格内容位于屏幕顶部。内容在导航栏后面 iOS 8:表格顶部和表格内容位于导航栏下方。内容在导航栏下方是否正确 NSLayoutConstraintitem:self.tableView,属性:.Top,relatedBy:.Equal,toItem:self.topLayoutGuide,属性:.Bottom,乘数:1,常数:0 iOS 7:表格顶部和表格内容位于导航栏下方。内容在导航栏下方是否正确 iOS 8:表的顶部位于导航栏的底部正确,但表的内容位于导航栏的下方,加上看起来像偏移的高度,再次不正确UITableView自上而下布局指南以编程方式插入内容,uitableview,swift,autolayout,Uitableview,Swift,Autolayout,由于我试图正确实现iAds,即共享ADBannerView的单个实例,因此我正在UIViewController中以编程方式创建UITableView,并将其添加到视图中。下面是我的UIViewController子类的几个片段: 从视图下载 iAds的实现增加了尝试和证明我的UITableView实现的正确性或错误性 如您所见,注释掉了3个约束。每种方法似乎都有不同的结果。除非被要求,否则我不会发布它们的截图,但我会描述它们 NSLayoutConstraintitem:self.tableV
我知道我可以做一个if-iOS7{…}else{…},但是感觉很脏,我感觉是我缺乏理解导致了这个问题,所以我想知道如何在iOS 7和8上进行这项工作,而不必进行版本检查,如果可能的话。我最终找到了一种方法。我不确定它到底有多黑客,但到目前为止,它在iOS 7.0-8.2上运行良好
override func viewDidLayoutSubviews() {
if self.tableView != nil {
// Setting both of these to 0 seems to fix some auto layout issues that crop up in iOS 7/8 depending on
// which item the layout is to, e.g., in iOS 8, having the UITableView's top be to the topLayoutGuide's bottom will
// cause a gap at the top of the UITableView, but this removes that gap and doesn't seem to affect iOS 7
self.tableView.contentInset = UIEdgeInsetsZero
self.tableView.scrollIndicatorInsets = UIEdgeInsetsZero
if self.showingiAd {
let delegate = UIApplication.sharedApplication().delegate as AppDelegate
if let bannerView = delegate.bannerView {
let bannerViewHeight = bannerView.frame.size.height
self.tableViewBottomLayoutConstraint.constant = -bannerViewHeight
}
}
}
}
func showiAds(animated: Bool) {
println("Show iAd")
if !self.showingiAd {
println("Showing iAd")
self.showingiAd = true
// Add the banner view below the content before it's then animated in to view
let delegate = UIApplication.sharedApplication().delegate as AppDelegate
let bannerView = delegate.bannerView
self.bannerBottomConstraint = NSLayoutConstraint(item: bannerView, attribute: .Bottom, relatedBy: .Equal, toItem: self.bottomLayoutGuide, attribute: .Top, multiplier: 1, constant: bannerView.frame.size.height)
if (bannerView.superview != self.view) {
bannerView.removeFromSuperview()
}
self.view.addSubview(bannerView)
self.view.addConstraints([
self.bannerBottomConstraint,
NSLayoutConstraint(item: bannerView, attribute: .Left, relatedBy: .Equal, toItem: self.view, attribute: .Left, multiplier: 1, constant: 0),
NSLayoutConstraint(item: bannerView, attribute: .Right, relatedBy: .Equal, toItem: self.view, attribute: .Right, multiplier: 1, constant: 0),
])
self.view.layoutIfNeeded()
// Only the changing of the value of the top of the banner is animated so it "slides in" from the bottom
self.bannerBottomConstraint.constant = 0
self.view.setNeedsUpdateConstraints()
UIView.animateWithDuration(animated ? 0.5 : 0, animations: { () -> Void in
// Calling layoutIfNeeded here will animate the layout constraint cosntant change made above
self.view.layoutIfNeeded()
}, completion: { (completed) -> Void in
if completed {
println("Completed animation")
}
})
}
}
func hideiAds() {
println("Hide iAd")
if self.self.showingiAd {
self.showingiAd = false
println("Hiding iAd")
let delegate = UIApplication.sharedApplication().delegate as AppDelegate
let bannerView = delegate.bannerView
if bannerView.superview == self.view {
bannerView.removeFromSuperview()
}
self.view.removeConstraint(self.tableViewBottomLayoutConstraint)
self.tableViewBottomLayoutConstraint = NSLayoutConstraint(item: self.tableView, attribute: .Bottom, relatedBy: .Equal, toItem: self.bottomLayoutGuide, attribute: .Top, multiplier: 1, constant: 0)
self.view.addConstraint(self.tableViewBottomLayoutConstraint)
}
}
override func viewDidLayoutSubviews() {
if self.tableView != nil {
// Setting both of these to 0 seems to fix some auto layout issues that crop up in iOS 7/8 depending on
// which item the layout is to, e.g., in iOS 8, having the UITableView's top be to the topLayoutGuide's bottom will
// cause a gap at the top of the UITableView, but this removes that gap and doesn't seem to affect iOS 7
self.tableView.contentInset = UIEdgeInsetsZero
self.tableView.scrollIndicatorInsets = UIEdgeInsetsZero
if self.showingiAd {
let delegate = UIApplication.sharedApplication().delegate as AppDelegate
if let bannerView = delegate.bannerView {
let bannerViewHeight = bannerView.frame.size.height
self.tableViewBottomLayoutConstraint.constant = -bannerViewHeight
}
}
}
}