单击搜索按钮时,导航栏的TitleView设置为搜索栏 并在搜索栏前面显示取消按钮。 但在iPad取消按钮不显示这是我的问题。
This is the Actual result on All iPhone Device
class MenuTableViewController: UITableViewController,UISearchBarDelegate{
//MARK: - Declare Variable
let searchBar = UISearchBar()
var image = UIImage()
var menuList:NSMutableArray = ["About Us","Packages","Locations","Hot News","Testimonials","FAQ's","Contact"]
let colorArray:[UIColor] = [UIColor.blackColor(),UIColor.blueColor(),UIColor.brownColor(),UIColor.cyanColor(),UIColor.purpleColor(),UIColor.greenColor(),UIColor.orangeColor()]
//MARK: - Outlet variable
//MARK: - Outlet Methods
//MARK: - ViewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.translucent = true
image = UIImage(named: "photo.png")!
self.navigationItem.titleView = UIImageView(image: image)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Search, target: self, action:(#selector(MenuTableViewController.searchData)))
self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "399-list1"), style: .Done, target: self, action: (#selector(MenuTableViewController.menuDisplay)))
self.searchBar.showsCancelButton = true
self.searchBar.delegate = self
}
//MRAK: - SerachBar Delegate Methods
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
print("Cancel")
self.navigationItem.titleView = UIImageView(image: image)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Search, target: self, action:(#selector(MenuTableViewController.searchData)))
}
//MARK: - Search Methods
func searchData() {
print("done search")
self.navigationItem.leftBarButtonItem = nil;
// add the search bar (which will start out hidden).
self.navigationItem.titleView = searchBar;
searchBar.becomeFirstResponder()
}
//MARK: - Menu Display Methods
func menuDisplay() {
print("display menu")
}