【问题标题】:UITableView top to topLayoutGuide programmatically content insetUITableView top to topLayoutGuide 以编程方式插入内容
【发布时间】:2014-11-19 18:56:35
【问题描述】:

由于我尝试“正确”实施 iAd(即共享 ADBannerView 的单个实例),我正在以编程方式在 UIViewController 内创建 UITableView 并将其添加到视图中。下面是我的UIViewController子类中的几个sn-ps:

来自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 上工作,而无需进行版本检查。

【问题讨论】:

    标签: uitableview swift autolayout


    【解决方案1】:

    我最终找到了一种方法。我不确定它到底有多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
                    }
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2011-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-22
      • 2020-03-13
      • 2014-03-03
      • 2015-06-30
      • 1970-01-01
      相关资源
      最近更新 更多