gpt4 book ai didi

UITableView 从顶部到 topLayoutGuide 以编程方式插入内容

转载 作者:行者123 更新时间:2023-11-28 09:17:11 24 4
gpt4 key购买 nike

由于我试图“正确地”实现 iAds(即共享 ADBannerView 的单个实例),我在 UIViewController 中以编程方式创建了一个 UITableView 并将其添加到 View 中。以下是我的 UIViewController 子类的几个片段:

来自 viewDidLoad

self.tableView = UITableView(frame: self.view.bounds, style: .Grouped)
self.tableView.allowsSelectionDuringEditing = true
self.tableView.registerNib(UINib(nibName: "TableViewCellWithSwitch", bundle: nil), forCellReuseIdentifier: "SliderCellIdentifier")
self.tableView.dataSource = self
self.tableView.setTranslatesAutoresizingMaskIntoConstraints(false)

self.view.addSubview(self.tableView)
self.tableViewBottomLayoutConstraint = NSLayoutConstraint(item: self.tableView, attribute: .Bottom, relatedBy: .Equal, toItem: self.bottomLayoutGuide, attribute: .Top, multiplier: 1, constant: 0)
self.view.addConstraints([
NSLayoutConstraint(item: self.tableView, attribute: .Left, relatedBy: .Equal, toItem: self.view, attribute: .Left, multiplier: 1, constant: 0),
//NSLayoutConstraint(item: self.tableView, attribute: .Top, relatedBy: .Equal, toItem: self.view, attribute: .Top, multiplier: 1, constant: 0),
//NSLayoutConstraint(item: self.tableView, attribute: .Top, relatedBy: .Equal, toItem: self.view, attribute: .Top, multiplier: 1, constant: self.topLayoutGuide.length),
//NSLayoutConstraint(item: self.tableView, attribute: .Top, relatedBy: .Equal, toItem: self.topLayoutGuide, attribute: .Bottom, multiplier: 1, constant: 0),
NSLayoutConstraint(item: self.tableView, attribute: .Right, relatedBy: .Equal, toItem: self.view, attribute: .Right, multiplier: 1, constant: 0),
self.tableViewBottomLayoutConstraint
])
// This must be called or the use of self.topLayoutGuide will not function
// See: https://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/index.html#//apple_ref/occ/instp/UIViewController/topLayoutGuide
self.view.layoutSubviews()

iAds 实现(添加以尝试证明我对 UITableView 的实现是对是错)

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)
}
}

如您所见,注释掉了 3 个约束条件。每一个似乎都有不同的结果。我不会发布它们的屏幕截图(除非有人要求),但我会描述它们。

NSLayoutConstraint(item: self.tableView, attribute: .Top, relatedBy: .Equal, toItem: self.view, attribute: .Top, multiplier: 1, constant: 0)

iOS 7:表格顶部和表格内容位于屏幕顶部。内容在导航栏后面

iOS 8:表格顶部和表格内容位于导航栏下方。内容在导航栏下方(正确)

NSLayoutConstraint(item: self.tableView, attribute: .Top, relatedBy: .Equal, toItem: self.view, attribute: .Top, multiplier: 1, constant: self.topLayoutGuide.length)

iOS 7:表格顶部和表格内容位于屏幕顶部。内容在导航栏后面

iOS 8:表格顶部和表格内容位于导航栏下方。内容在导航栏下方(正确)

NSLayoutConstraint(item: self.tableView, attribute: .Top, relatedBy: .Equal, toItem: self.topLayoutGuide, attribute: .Bottom, multiplier: 1, constant: 0)

iOS 7:表格顶部和表格内容位于导航栏下方。内容在导航栏下方(正确)

iOS 8:表格顶部位于导航栏底部(正确),但表格内容位于导航栏下方,再加上(看起来像)偏移量的高度(不正确)

我知道我可以做一个 if iOS7 {...} else {...},但这感觉很脏,我觉得这是我缺乏理解这就是导致这个问题的原因,所以我想弄清楚如何在 iOS 7 和 8 上进行这项工作,而无需求助于版本检查,如果可能的话。

最佳答案

我最终找到了执行此操作的方法。我不确定它有多 hacky,但它目前在 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
}
}
}
}

关于UITableView 从顶部到 topLayoutGuide 以编程方式插入内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27024755/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com