gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:56:48 26 4
gpt4 key购买 nike

我正在尝试模拟日历应用程序中的搜索栏并发现它非常困难,尽管许多人已经在 SO 和其他地方提出了这个问题并且已经提供了许多一半的答案。 (我需要支持 IOS 7)。

主要要求是

1) 有一个搜索栏按钮。

2) 当按下上面的按钮时,导航栏中会出现一个带有取消的搜索栏。

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

完成 2) 是困难的部分。

要让搜索栏显示在导航栏中,而不是您应该设置的其他地方self.searchDisplayController.displaysSearchBarInNavigationBar= true 作为 here ;

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

显示取消按钮的代码应该是:

self.searchDisplayController.searchBar.showsCancelButton = YES;

这不能与将搜索栏放在导航栏中一起使用。

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

有没有成功创建此 UX 的人可以描述在 IOS 7.0 中模拟日历应用程序中的搜索栏所需的所有步骤?

最佳答案

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

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

关于ios - 在日历应用程序中模拟搜索栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34623822/

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