我有两个可以通过标签栏访问的场景,在场景1上有一个搜索栏。 我面临的问题是在搜索时是否切换到下载选项卡 -
现在,当我点击下载标签时,导航栏就会消失。
这是第一个屏幕的视图控制器 -
import UIKit
import Alamofire
import SwiftyJSON
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating, UISearchBarDelegate{
//MARK: Variables
var papers = [Paper]()
var filteredPapers = [Paper]()
let searchController = UISearchController(searchResultsController: nil)
// MARK: Outlets
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
@IBOutlet var table: UITableView!
@IBOutlet weak var loadingMessageLabel: UILabel!
@IBOutlet weak var retryButton: UIButton!
//MARK: Actions
@IBAction func retryButton(sender: UIButton) {
self.loadingMessageLabel.hidden = false
self.loadingMessageLabel.text = "While the satellite moves into position..."
self.activityIndicator.hidden = false
self.activityIndicator.startAnimating()
self.retryButton.hidden = true
self.getPapersData()
}
// MARK: Table View
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// If in searching mode, then return the number of results else return the total number
// if searchController.active && searchController.searchBar.text != "" {
if searchController.active {
return filteredPapers.count
}
return papers.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let paper: Paper
// if searchController.active && searchController.searchBar.text != "" {
if searchController.active {
paper = filteredPapers[indexPath.row]
} else {
paper = papers[indexPath.row]
}
if let cell = self.table.dequeueReusableCellWithIdentifier("Cell") as? PapersTableCell {
cell.initCell(paper.name, detail: paper.detail)
print(cell)
return cell
}
return PapersTableCell()
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
let downloadButton = UITableViewRowAction(style: .Normal, title: "Download") { action, index in
var url = String(self.papers[indexPath.row].url)
url = url.stringByReplacingOccurrencesOfString(" ", withString: "%20")
print(url)
let destination = Alamofire.Request.suggestedDownloadDestination(directory: .DocumentDirectory, domain: .UserDomainMask)
// Spinner in cell
// var selectCell = self.table.cellForRowAtIndexPath(indexPath) as? PapersTableCell
// selectCell!.downloadSpinner.hidden = false
// Dismiss the download button
self.table.editing = false
Alamofire.download(.GET, url, destination: destination).response { _, _, _, error in
if let error = error {
print("Failed with error: \(error)")
} else {
print("Downloaded file successfully")
}
// selectCell?.downloadSpinner.hidden = true
}
}
downloadButton.backgroundColor = UIColor(red:0.30, green:0.85, blue:0.39, alpha:1.0)
return [downloadButton]
}
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// the cells you would like the actions to appear needs to be editable
return true
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
// you need to implement this method too or you can't swipe to display the actions
}
// MARK: Search
func filterContentForSearchText(searchText: String, scope: String = "All") {
filteredPapers = papers.filter { paper in
let categoryMatch = (scope == "All") || (paper.exam == scope)
return categoryMatch && paper.name.lowercaseString.containsString(searchText.lowercaseString)
}
table.reloadData()
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
let searchBar = searchController.searchBar
let scope = searchBar.scopeButtonTitles![searchBar.selectedScopeButtonIndex]
filterContentForSearchText(searchController.searchBar.text!, scope: scope)
}
func searchBar(searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
filterContentForSearchText(searchBar.text!, scope: searchBar.scopeButtonTitles![selectedScope])
}
// MARK: Defaults
override func viewDidLoad() {
super.viewDidLoad()
self.getPapersData()
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
table.tableHeaderView = searchController.searchBar
searchController.searchBar.scopeButtonTitles = ["All", "ST1", "ST2", "PUT", "UT"]
searchController.searchBar.delegate = self
activityIndicator.startAnimating()
}
override func viewWillDisappear(animated: Bool) {
// if searchController.active {
self.searchController.resignFirstResponder()
// }
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: API call
func getPapersData(){
Alamofire.request(.GET, "http://silive.in/bytepad/rest/api/paper/getallpapers?query=")
.responseJSON { response in
self.activityIndicator.stopAnimating()
self.activityIndicator.hidden = true
// If the network works fine
if response.result.isFailure != true {
self.loadingMessageLabel.hidden = true
self.table.hidden = false
//print(response.result) // result of response serialization
let json = JSON(response.result.value!)
for item in json {
// Split the title on the . to remove the extention
let title = item.1["Title"].string!.characters.split(".").map(String.init)[0]
let category = item.1["ExamCategory"].string
let url = item.1["URL"].string
let detail = item.1["PaperCategory"].string
let paper = Paper(name: title, exam: category!, url: url!, detail: detail!)
self.papers.append(paper)
}
self.table.reloadData()
}
// If the network fails
else {
self.retryButton.hidden = false
self.loadingMessageLabel.text = "Check your internet connectivity"
}
}
}
}
答案 0 :(得分:12)
我在iOS12中遇到了这个问题,并通过将托管搜索控制器的ViewController的definePresentationContext设置为true来解决。
self.definesPresentationContext = true
答案 1 :(得分:8)
这是因为选项卡可能没有单独的导航控制器,可以为您提供黑屏。为了单独维护导航层次结构,您应该将 uinavigationcontroller 嵌入 uiviewcontroller ,并从IB检查 Top Bar 作为不透明导航栏而不是推断。希望这可以帮助。 Cheerio
答案 2 :(得分:4)
在幕后,正在呈现搜索控制器。这是返回时导致黑屏的原因。
最简单的方法是覆盖UISearchController并在其isActive=false
(https://stackoverflow.com/a/39212080/215748)中设置viewDidDisappear
。它有效,但我发现这个解决方案存在一些可用性问题。我没有追求它,所以它们可能很容易克服。
在远离视图控制器之前调用以下命令:
searchController.dismiss(animated: false, completion: nil)
将此添加到viewDidDisappear
。
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
dismissSearch()
}
func dismissSearch() {
searchController.dismiss(animated: false, completion: nil)
}
不幸的是,当您切换标签时,viewDidDisappear
无法调用,因为正在显示搜索控制器。它收到了viewDidDisappear
。要解决此问题,您可以继承UITabBarController
并实施UITabBarControllerDelegate
。
// don't forget to set the delegate
extension TabBarController: UITabBarControllerDelegate {
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if let navigationController = viewControllers?[selectedIndex] as? UINavigationController {
for myController in navigationController.viewControllers.flatMap({ $0 as? MyTableViewController }) {
myController.dismissSearch()
}
}
return true
}
答案 3 :(得分:3)
看起来您的UISearchController
所附加的视图会从视图层次结构中删除。您可以将UISearchController
视为在开始搜索时以模态方式呈现,definesPresentationContext
属性指示哪个UIViewController
将是呈现它的人more on this。 / p>
您可以在可能重复的问题的答案中获得更多详细信息:https://stackoverflow.com/a/37357242/300131
答案 4 :(得分:2)
只需将UiNavigationcontroller添加到下载选项卡的uiviewcontroller,它将解决问题停电和导航栏隐藏
答案 5 :(得分:1)
如果你移动这段代码怎么办
self.activityIndicator.stopAnimating()
self.activityIndicator.hidden = true
到你的结果之后?像这样:
// If the network works fine
if response.result.isFailure != true {
self.activityIndicator.stopAnimating()
self.activityIndicator.hidden = true
(如果有效,你也需要把它包含在你的另一边......)
// If the network fails
else {
self.activityIndicator.stopAnimating()
self.activityIndicator.hidden = true
self.retryButton.hidden = false
self.loadingMessageLabel.text = "Check your internet connectivity"
}