Ios 在日历应用程序中模拟搜索栏

Ios 在日历应用程序中模拟搜索栏,ios,storyboard,searchbar,searchdisplaycontroller,Ios,Storyboard,Searchbar,Searchdisplaycontroller,我试图模仿Calendar应用程序中的搜索栏,发现它出人意料地困难,尽管许多人在某地和其他地方提出了这个问题,并且给出了许多一半的答案。(我需要支持IOS 7) 主要要求是 1) 有一个搜索栏按钮 2) 按下上述按钮时,导航栏中会出现带有“取消”的搜索栏 要完成1)只需在导航栏上放置一个栏按钮项。没问题 完成任务是困难的部分 要使搜索栏显示在导航栏中,而不是其他位置,您只需设置 self.searchDisplayController.displaysSearchBarInNavigationB

我试图模仿Calendar应用程序中的搜索栏,发现它出人意料地困难,尽管许多人在某地和其他地方提出了这个问题,并且给出了许多一半的答案。(我需要支持IOS 7)

主要要求是

1) 有一个搜索栏按钮

2) 按下上述按钮时,导航栏中会出现带有“取消”的搜索栏

要完成1)只需在导航栏上放置一个栏按钮项。没问题

完成任务是困难的部分

要使搜索栏显示在导航栏中,而不是其他位置,您只需设置
self.searchDisplayController.displaysSearchBarInNavigationBar=true
as

我可以让搜索栏出现在导航栏中,但没有取消按钮

显示取消按钮的代码应为:

self.searchDisplayController.searchBar.showsCancelButton = YES;
这与将搜索栏放置在导航栏中不起作用

最后,与searchDisplayController不同,一个叫做搜索栏的东西有一个叫做.hidden的属性。将搜索栏和search displaycontroller拖到视图后,我已为此创建了一个outlet属性,并尝试更改此属性,但未成功。(将其从true更改为false对输出没有明显影响。)


有没有人成功创建了这个用户体验,能够描述在IOS 7.0中模拟日历应用程序中的搜索栏所需的所有步骤?

下面的代码只是为了说明这个想法。如果你想使用它,你必须根据你的需要调整它,注意方向的变化并添加一个表视图。还有一些常数可以用更好的东西代替

class ViewController: UIViewController, UISearchBarDelegate {
    let searchBar = UISearchBar()
    private var searchBarVisible = false

    override func viewDidLoad() {
        super.viewDidLoad()

        searchBar.showsCancelButton = true
        searchBar.delegate = self

        let height: CGFloat = 44
        let window = UIApplication.sharedApplication().keyWindow!
        self.searchBar.frame = CGRectMake(0, -height, window.frame.width, height)
    }

    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        self.dismissSearchBarAnimated(true)
    }

    func showSearchBarAnimated(animated: Bool) {
        if !searchBarVisible {
            let window = UIApplication.sharedApplication().keyWindow!
            window.addSubview(searchBar)
            searchBar.setNeedsLayout()
            let duration = animated ? 0.2 : 0 // 0 executes immediately
            self.searchBar.becomeFirstResponder()
            UIView.animateWithDuration(duration) { () -> Void in
                self.searchBar.frame = CGRectMake(0, 20, window.frame.width, self.searchBar.frame.height)
            }
            searchBarVisible = true
        }
    }

    func dismissSearchBarAnimated(animated: Bool) {
        let window = UIApplication.sharedApplication().keyWindow!
        let duration = animated ? 0.2 : 0 // 0 executes immediately
        self.searchBar.resignFirstResponder()
        UIView.animateWithDuration(duration,
            animations: { () -> Void in
                self.searchBar.frame = CGRectMake(0, -self.searchBar.frame.height, window.frame.width, self.searchBar.frame.height)
            }) {(completed) -> Void in
                self.searchBar.removeFromSuperview()
                self.searchBarVisible = false
        }
    }

    @IBAction func actionSearchButton(sender: AnyObject) {
        self.showSearchBarAnimated(true)
    }

    //MARK: - UISearchBarDelegate
    func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
        NSLog("Should search for '\(searchText)'.")
    }

    func searchBarCancelButtonClicked(searchBar: UISearchBar) {
        self.dismissSearchBarAnimated(true)
    }

    func positionForBar(bar: UIBarPositioning) -> UIBarPosition {
        return .TopAttached
    }
}

这似乎是你12小时前问的问题的副本。这有什么不同?如果你有更多的细节,为什么不更新之前的问题?这是不同的。这个问题是关于用代码创建搜索栏的。这是关于模拟日历应用程序的。可以在情节提要中拖动实际的搜索栏。问题是,一旦在storyboard.FYI中创建了它,该如何控制它,Messenger应用程序中也使用了它。