简体   繁体   English

带有UITableviewController Swift顶部标签的UIView

[英]UIView with labels at top of UITableviewcontroller Swift

I am trying to keep a UIView with a label, segmentedControl and a button at the top of my UITableViewController as it scrolls. 我试图在滚动时在UITableViewController的顶部保留一个带有标签,segmentedControl和按钮的UIView。 I cannot use a ViewController with UITableView because my TableViewCells are static with UITextFields in them. 我无法将ViewController与UITableView一起使用,因为我的TableViewCells是静态的,其中包含UITextFields。 I know this can be done via ViewController and ContainerViewController, however, this makes things complicated with the amount of data that needs to get passed between the views. 我知道这可以通过ViewController和ContainerViewController来完成,但是,这使需要在视图之间传递的数据量变得复杂。 I am looking for a scrollViewDidScroll solution in swift. 我正在寻找swiftViewDidScroll解决方案。

Here is what I have so far. 这是我到目前为止所拥有的。 I have the View already created in storyboards (maybe that is part of the problem.) labelView is connected via outlet. 我已经在情节提要板上创建了View(也许这是问题的一部分。)labelView通过插座连接。

    override func scrollViewDidScroll(scrollView: UIScrollView) {
    var rect = self.labelView.frame
    rect.origin.y = min(0,scrollView.contentOffset.y + scrollView.contentInset.top);
    self.labelView.frame = rect;
}

If you have just one section then you may try adding the view in section header. 如果您只有一个部分,则可以尝试将视图添加到部分标题中。 yeah you have to change lot of thing but it will 100% work 是的,你必须改变很多事情,但是它将100%起作用

I figured it out. 我想到了。

First I put a dummy blank view where I wanted my view to be. 首先,我将虚拟的空白视图放置在希望放置的位置。 I did this otherwise it sets itself on top the tableview. 我这样做了,否则它将自己置于tableview的顶部。 I am sure you could also off set in code but this seemed easier. 我相信您也可以在代码中设置偏移量,但这似乎更容易。 Then I created a separate view outside of the tableViewController with my labels and button set up. 然后,我在tableViewController外部创建了一个单独的视图,并设置了标签和按钮。 Connected this view via outlet. 通过插座连接此视图。 Here is the code to add the view and then keep its position when scrolling. 这是添加视图并在滚动时保持其位置的代码。

@IBOutlet var labelView: UIView!

override func viewDidLayoutSubviews() {
    self.view.addSubview(labelView)
    labelView.frame.size.width = self.view.frame.size.width
}

override func scrollViewDidScroll(scrollView: UIScrollView) {
    var rect = self.labelView.frame
    rect.origin.y = max(0,scrollView.contentOffset.y + scrollView.contentInset.top)
    self.labelView.frame = rect
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM