Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UISearchBar排除动画问题_Ios_Swift_Uinavigationbar_Uisearchbar - Fatal编程技术网

Ios UISearchBar排除动画问题

Ios UISearchBar排除动画问题,ios,swift,uinavigationbar,uisearchbar,Ios,Swift,Uinavigationbar,Uisearchbar,我有一个带搜索栏和搜索显示控制器的UITableViewController: 一切都是默认的。导航栏是半透明的。单击搜索栏时,导航栏会向上滑动。现在,当您关闭搜索时,它会滑回,这会在搜索和导航栏之间产生一个白色的间隙: 你知道是什么原因造成的吗?如何解决?我使用的是swift、iOS8.1和xcode6.1 我唯一能做的就是将表视图的视图背景设置为蓝色。但这有一个负面的副作用,即当表视图为空时,所有内容都将为蓝色。我认为“白色间隙”是表视图控制器的背景色。我想您可以更改表视图控制器的背景

我有一个带搜索栏和搜索显示控制器的UITableViewController:

一切都是默认的。导航栏是半透明的。单击搜索栏时,导航栏会向上滑动。现在,当您关闭搜索时,它会滑回,这会在搜索和导航栏之间产生一个白色的间隙:

你知道是什么原因造成的吗?如何解决?我使用的是swift、iOS8.1和xcode6.1



我唯一能做的就是将表视图的视图背景设置为蓝色。但这有一个负面的副作用,即当表视图为空时,所有内容都将为蓝色。

我认为“白色间隙”是表视图控制器的背景色。我想您可以更改表视图控制器的背景颜色以匹配搜索栏或导航栏,但整个表视图控制器也将是该颜色。

状态栏的颜色是由ExtendedLayoutCludeSopaqueBars属性引起的。 默认设置为“是”。将其设置为“否”,则状态栏将始终具有相同的颜色


希望它能对您有所帮助。

将搜索栏更改为灰色(即背景色)。这可能会帮助您尝试将UISearchBar放置在表视图之外,而不是作为其子视图。这可能是一个小的设计更改,但动画效果要好得多。

那里的白色是导航控制器的背景色

1) 在故事板中,指定导航控制器的类,并将其背景颜色设置为您想要的任何颜色。。。就像我在viewDidLoad()中的示例一样

2) 设置表格视图的背景颜色。默认情况下,它将与基础nav控制器背景相同(在情节提要中执行此操作是可以的)

如果您想要与导航栏相同的颜色

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    view.backgroundColor = navigationBar.tintColor
}

根据您的问题,我创建了类似的示例演示,并得出结论,导航栏和搜索栏之间出现空白是因为您设置了搜索栏控制器而不是搜索栏

我做了更多的挖掘,了解到这是搜索栏的默认行为

在使用搜索栏控制器时,表外视图还将显示导航栏和搜索栏之间的空间

您需要在UItableView外部的UIView上使用UISearch栏,而不是使用UISearchBarController

您可以使用与搜索栏类似的方式显示和隐藏导航栏,方法如下:使用动画隐藏和显示导航栏

编辑:
现在,您不需要将背景色设置为导航栏颜色,当SearchBarCancelButtonClick时,它不会像以前那样具有平滑的动画

但这将不会像以前那样具有平滑动画
因此,您可以将该背景色设置为导航栏颜色

希望这可以解决您的问题。

我可以找到一种方法来避免这种差距,这显然是一个动画时间的问题

在FirstTableViewController.swift中,您需要注册为UISearchBar的代理,然后在使用代理方法SearchBartextdidediting(文本编辑结束后)取消搜索栏时(尽可能早)触发导航栏的出现:

这是您的最后一个swift文件,其中添加了2项内容:第3行的UISearchBarDelegate和末尾的SearchBartextdidediting函数:

import UIKit

class FirstTableViewController: UITableViewController, UISearchBarDelegate {

    var entries: NSArray = ["Foo", "Bar"];

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.entries.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

        cell.textLabel?.text = entries[indexPath.row] as? String

        return cell
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        performSegueWithIdentifier("test", sender: nil)
    }

    // Fix searchbar disappear bug

    func searchDisplayControllerDidEndSearch(controller: UISearchDisplayController) {
        self.tableView.insertSubview(self.searchDisplayController!.searchBar, aboveSubview: self.tableView)
    }


    func searchBarTextDidEndEditing(searchBar: UISearchBar) {
        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }


}
编辑-通过调整IB中的适当布局选项,两个视图控制器都可以工作

第一:表格视图的3个选中选项位于图形中:不透明+清除图形上下文+自动调整子视图的大小

第二:对于ViewController本身,选中的扩展边选项是:在底部栏下+在不透明栏下

第三:上面关于UISearchBar委派的代码

import UIKit

class NavControllerViewController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.tintColor = UIColor.redColor()
        view.backgroundColor = view.tintColor
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}


class FirstTableViewController: UITableViewController, UISearchBarDelegate {

    var entries: NSArray = ["Foo", "Bar"];

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.entries.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

        cell.textLabel?.text = entries[indexPath.row] as? String

        return cell
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        performSegueWithIdentifier("test", sender: nil)
    }

}

class SecondTableViewController: UITableViewController, UISearchBarDelegate {

    var entries: NSArray = ["Foo", "Bar"]

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.entries.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = self.tableView.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath) as UITableViewCell

        cell.textLabel?.text = entries[indexPath.row] as? String

        return cell
    }

}
没有必要做任何特殊的把戏。正如我在第一个答案中所写的,白色间隙只是NavigationController主视图的背景色


这段代码对我来说很有用,我现在将tintColor和backgroundColor设置为红色,向您显示…后面发生了什么。

+1您的右侧,当我将表格背景更改为蓝色时,间隙不可见。但是,我需要表格背景为白色…为什么不将UITableViewController替换为UIViewController呢。将contentView的背景设置为蓝色。然后将带有约束的UITableView添加到顶部布局指南中。这将使您在导航栏下有一个蓝色背景,但也使您有一个白色背景的表。这实际上是一个很好的解决方案,但故事板不允许将搜索栏拖到表视图上方。@artworkadシ 大多数时候,由于某些设计元素,我使用普通的UIViewController并将UITableView拖到上面,然后手动连接委托/数据源。在这种情况下,您还必须连接搜索显示控制器。通过这种方式,您可以自定义视图,而不是使用UITableViewController。不幸的是,这不起作用。我唯一能把空间变成蓝色的方法就是把整个桌子的背景变成蓝色。我在电脑上检查了一下,它工作了!它现在运行,我有红色的差距,蓝色的导航栏和黄色的空表。你把两种颜色都定了吗?您必须在导航控制器中设置view.backgroundColor(仅以编程方式)以及表的backgroundColor。如果您愿意,我可以与您共享一个小项目。请通过github进行共享。例如,您的解决方案对我有效,但仅适用于第一个视图。如果从一个表视图导航到另一个表视图,则第二个视图不起作用。在这个问题上,我提供另一个200人的悬赏。检查我的代码版本:它适用于第一个表视图,但不适用于第二个表视图。检查此项我创建它是为了演示我的问题。您好,我检查了您创建的演示。正如我所期望的,您已经使用了搜索栏控制器,并且设置在tableView中。我已经添加了演示,您可以查看。你所需要的一切
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
    UIView.animateWithDuration(0.3, animations: { () -> Void in
        searchBar.showsCancelButton = false
        searchBar.resignFirstResponder()
           // Uncomment below line for smooth animation effect.
           // self.navigationController!.navigationBarHidden = false
        }, completion: { finished in
            //comment below lines is you want smooth animation.
            self.navigationController!.navigationBar.alpha = 1
            self.navigationController!.navigationBarHidden = false
    })
}
import UIKit

class FirstTableViewController: UITableViewController, UISearchBarDelegate {

    var entries: NSArray = ["Foo", "Bar"];

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.entries.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

        cell.textLabel?.text = entries[indexPath.row] as? String

        return cell
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        performSegueWithIdentifier("test", sender: nil)
    }

    // Fix searchbar disappear bug

    func searchDisplayControllerDidEndSearch(controller: UISearchDisplayController) {
        self.tableView.insertSubview(self.searchDisplayController!.searchBar, aboveSubview: self.tableView)
    }


    func searchBarTextDidEndEditing(searchBar: UISearchBar) {
        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }


}
import UIKit

class NavControllerViewController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.tintColor = UIColor.redColor()
        view.backgroundColor = view.tintColor
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}


class FirstTableViewController: UITableViewController, UISearchBarDelegate {

    var entries: NSArray = ["Foo", "Bar"];

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.entries.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

        cell.textLabel?.text = entries[indexPath.row] as? String

        return cell
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        performSegueWithIdentifier("test", sender: nil)
    }

}

class SecondTableViewController: UITableViewController, UISearchBarDelegate {

    var entries: NSArray = ["Foo", "Bar"]

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.entries.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = self.tableView.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath) as UITableViewCell

        cell.textLabel?.text = entries[indexPath.row] as? String

        return cell
    }

}