I am having a hard time creating a UIFocusGuide that will jump from the UITableView to a UIButton. Here is the debugger context screenshot:
And here is the implementation:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
// add the focus guide
self.view.addLayoutGuide(focusGuide)
// add the anchors
self.focusGuide.leftAnchor.constraintEqualToAnchor(self.button.leftAnchor).active = true
self.focusGuide.topAnchor.constraintEqualToAnchor(self.tableView.topAnchor).active = true
self.focusGuide.widthAnchor.constraintEqualToAnchor(self.button.widthAnchor).active = true
self.focusGuide.heightAnchor.constraintEqualToAnchor(self.tableView.heightAnchor).active = true
}
override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
guard let nextFocusedView = context.nextFocusedView else { return }
switch nextFocusedView {
case self.button:
self.focusGuide.preferredFocusedView = self.button
case self.tableView:
self.focusGuide.preferredFocusedView = self.tableView
default:
self.focusGuide.preferredFocusedView = nil
}
}
The didUpdateFocusInContext function is never getting called when I am at the middle item of the UITableView or the end of the UITableView.
Add the focus guide to the button not self.view. You don't need override didUpdateFocusInContext For example:
var focusGuide = UIFocusGuide()
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
// add the focus guide
self.button.addLayoutGuide(focusGuide)
// add the anchors
self.focusGuide.leftAnchor.constraintEqualToAnchor(self.button.leftAnchor).active = true
self.focusGuide.topAnchor.constraintEqualToAnchor(self.tableView.topAnchor).active = true
self.focusGuide.widthAnchor.constraintEqualToAnchor(self.button.widthAnchor).active = true
self.focusGuide.heightAnchor.constraintEqualToAnchor(self.tableView.heightAnchor).active = true
}
Related
I would like to add a button to the left of the search bar. However, I don't want the the scope titles to shift over as well, leaving me with a gap somewhere. I believe my options to be:
remove scope bar, use UISegmentedControl below, and add a button in
modify the UISearchBarClass with a button
?? container view as header, includes button and search controller ??
Here is my code, refactored to use a UIViewController for easy modification. Note that I am using the SnapKit libary for constraints (table cell logic removed). How can I accomplish this?
class TeamSearchController: UIViewController {
let tableView = UITableView()
var searchBar = UISearchBar()
override func viewDidLoad() {
super.viewDidLoad()
initUI()
}
func initUI() {
self.view.addSubview(tableView)
initSearchController()
initTableView()
}
func initSearchController() {
// searchController.dimsBackgroundDuringPresentation = false
searchBar.placeholder = "Search for a team here..."
searchBar.delegate = nil
searchBar.sizeToFit()
searchBar.showsScopeBar = true
searchBar.scopeButtonTitles = ["Cross Country", "Track"]
// searchController.definesPresentationContext = true
}
func initTableView() {
tableView.dataSource = nil
tableView.delegate = nil
tableView.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
tableView.tableHeaderView = searchBar
tableView.estimatedRowHeight = 40
tableView.rowHeight = UITableView.automaticDimension
}
}
Current Status:
i want to disable right cancel button when a tap at search Bar.
Because of using Google Place search i put i should use searchController?.searchBar
I try to disable cancel button at
func presentSearchController(_ searchController: UISearchController) {
searchController.searchBar.showsCancelButton = false
}
but when i tap a searchBar i see how cancel button appear and disappear, thats looks ugly
Please give me advice!
override func viewDidLoad() {
super.viewDidLoad()
resultsViewController = GMSAutocompleteResultsViewController()
resultsViewController?.delegate = self
searchController = UISearchController(searchResultsController: resultsViewController)
searchController?.searchResultsUpdater = resultsViewController
searchController?.searchBar.sizeToFit()
navigationItem.titleView = searchController?.searchBar
searchController?.searchBar.placeholder = searchBarPlaceholderText
searchController?.searchBar.tintColor = #colorLiteral(red: 0.1019607843, green: 0.5490196078, blue: 1, alpha: 1)
searchController?.searchBar.delegate = self
searchController?.delegate = self
searchController?.searchBar.searchBarStyle = .prominent
definesPresentationContext = true
mapView.delegate = self
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
checklocationAuthorizationStatus()
locationManager.requestWhenInUseAuthorization()
locationManager.requestLocation()
locationManager.startUpdatingLocation()
}
func presentSearchController(_ searchController: UISearchController) {
searchController.searchBar.showsCancelButton = false
}
You can create a custom class and subclass UISearchBar and UISearchViewController.
For example:-
class CustomizedSearchBar: UISearchBar {
override func layoutSubviews() {
super.layoutSubviews()
setShowsCancelButton(false, animated: false)
}
}
Now Create Object of the CustomizedSearchBar and use it within other viewController.
Or you can create a customized searchViewController as follows:
class CustomizedSearchController: UISearchController, UISearchBarDelegate {
lazy var _searchBar: CustomSearchBar = {
[unowned self] in
let result = CustomSearchBar(frame: CGRectZero)
result.delegate = self
return result
}()
override var searchBar: UISearchBar {
get {
return _searchBar
}
}
}
Please follow this link for more detail information.
I have implemented searchBar using UISearchController using following code -
var searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search here..."
definesPresentationContext = true
searchController.searchBar.delegate = self
searchController.searchBar.sizeToFit()
if #available(iOS 11.0, *) {
self.navigationItem.searchController = searchController
} else {
// Fallback on earlier versions
navigationItem.titleView = searchController.searchBar
navigationItem.titleView?.layoutSubviews()
}
Now I have two issues-
SearchBar comes below the navigationBar(See the image attached), how do I get the searchBar on top of NavigationBar that used to come when we implement searchBar with UISearch bar.
The cancel button is not coming on the right side of search bar.
I don't think you can do this natively. But you can activate the search bar when you open the menu (dont forget to set searchController.hidesNavigationBarDuringPresentation to true):
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
searchController.isActive = true
}
But it will hide the UINavigationBar so this is not what you really want. So, maybe better, you can create a custom navigation bar and hide the native one. Here is a quick example:
1 - Create a swift a xib file NavigationBarView with an horizontal UIStackView, a back UIButton with a fixed width and a UISearchBar:
class NavigationBarView: UIView {
var backAction: (()->Void)?
#IBOutlet weak var searchBarView: UISearchBar!
override func awakeFromNib() {
super.awakeFromNib()
// Customize your search bar
self.searchBarView.showsCancelButton = true
}
#IBAction func backButtonPressed(_ sender: Any) {
self.backAction?()
}
}
2 - Instead of using a UITableViewController, create a UIViewController with a vertical UIStackView which contains a view with a fixed height of 64 and a UITableView:
class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {
#IBOutlet weak var tableView: UITableView!
#IBOutlet weak var containerView: UIView!
let navigationBarView: NavigationBarView = NavigationBarView.viewFromNib() // Custom helper to instantiate a view, see below
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.isHidden = true // hide the native UINavigationBar
self.navigationBarView.backAction = {
self.navigationController?.popViewController(animated: true)
}
self.navigationBarView.searchBarView.delegate = self
self.navigationBarView.add(in: self.containerView) // Custom helper to put a view in a container view, see below
// Other stuff
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
}
Here is my helpers:
extension UIView {
static public func viewFromNib <GenericView: UIView> () -> GenericView {
let className = String(describing: self)
guard let instance = UINib(nibName: className, bundle: nil)
.instantiate(withOwner: nil, options: nil).first as? GenericView else {
// If this happens, it means the xcodeproj is broken
fatalError("Ho no its broken!")
}
return instance
}
func add(in superView: UIView) {
self.translatesAutoresizingMaskIntoConstraints = false
superView.addSubview(self)
self.topAnchor.constraint(equalTo: superView.topAnchor).isActive = true
self.bottomAnchor.constraint(equalTo: superView.bottomAnchor).isActive = true
self.leftAnchor.constraint(equalTo: superView.leftAnchor).isActive = true
self.rightAnchor.constraint(equalTo: superView.rightAnchor).isActive = true
}
}
Yo can try below code and please let me know if you are facing any issue.
if self.searchController != nil {
self.searchController.isActive = false
}
isSearching = true
self.searchController = UISearchController(searchResultsController: nil)
self.searchController.searchResultsUpdater = self
self.searchController.delegate = self
self.searchController.searchBar.delegate = self
self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController.dimsBackgroundDuringPresentation = false
self.navigationItem.titleView = searchController.searchBar
self.definesPresentationContext = false
self.searchController.searchBar.returnKeyType = .done
There is a property for this
searchController.hidesNavigationBarDuringPresentation = true
There is a gap, so it might be a white text Canel button. ou can know it for sure in Debugger Navigator (Cmd+7) -> View UI Hierarcy. White button text might be caused by custom navigation bar style
I'm trying to add a UIViewSearchController to ad UIView however it does not seem to show? So far I've created a subclass of UISearchController and UISearchBar. I've done that to remove the cancel button, from the searchController. However since i added these custom classes the searchBar does not show in the View? How come this that?
viewDidLoad in viewcontroller
//SearchController
backSearchView.backgroundColor = UIColor.clearColor()
self.resultSearchController = ({
let controller = MySearchController(searchResultsController: nil)
controller.hidesNavigationBarDuringPresentation = false
controller.dimsBackgroundDuringPresentation = false
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
controller.searchBar.frame = CGRectMake(8, 8, self.view.frame.width-16, 44)
controller.searchBar.setShowsCancelButton(false, animated: false)
controller.searchBar.backgroundImage = UIImage()
controller.searchBar.placeholder = "Søg"
controller.searchBar.returnKeyType = UIReturnKeyType.Default
controller.searchBar.delegate = self
let textFieldInsideSearchBar = controller.searchBar.valueForKey("searchField") as? UITextField
textFieldInsideSearchBar?.backgroundColor = UIColor.whiteColor()
textFieldInsideSearchBar?.clipsToBounds = true
textFieldInsideSearchBar?.layer.cornerRadius = 1
textFieldInsideSearchBar?.layer.borderWidth = 2
textFieldInsideSearchBar?.layer.borderColor = UIColor.whiteColor().CGColor
textFieldInsideSearchBar?.textColor = UIColor.darkGrayColor()
backSearchView.addSubview(controller.searchBar)
return controller
})()
SearchController subclass
class MySearchController: UISearchController, UISearchBarDelegate {
lazy var customSearchBar: NoCancelSearchBar = {
[unowned self] in
let result = NoCancelSearchBar(frame:CGRectZero)
result.delegate = self
return result
}()
override var searchBar: UISearchBar {
get {
return customSearchBar
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
SearchBar subclass
class NoCancelSearchBar: UISearchBar {
override func layoutSubviews() {
self.showsCancelButton = false
}
}
The error and the problem:
WARNING: More than 10 instances of ADBannerView or ADInterstitialView currently exist. This is a misuse of the iAd API, and ad performance will suffer as a result. This message is printed only once.
my implementation of the adView:
var adView = ADBannerView()
override func viewDidLoad() {
super.viewDidLoad()
adView.frame = CGRectOffset(adView.frame, 0, self.view.bounds.height - adView.bounds.height)
adView.sizeToFit()
self.view .addSubview(adView)
adView.alpha = 0.001
adView.delegate = self
}
i have looked on the internet and i found that i need to implement the "viewWillDisappear" and i did so:
my first try:
override func viewWillDisappear(animated: Bool) {
adView.removeFromSuperview()
adView.delegate = nil
}
and the second:
override func viewWillDisappear(animated: Bool) {
for view in self.view.subviews {
view.removeFromSuperview()
}
adView.removeFromSuperview()
adView.delegate = nil
}
one more thing, it says that it will just be displayed once but i get the error every time i run the app on my phone
After some search i came to this solution :
let adView = ADBannerView()
override func viewDidLoad() {
super.viewDidLoad()
adView.frame = CGRectOffset(adView.frame, 0, self.view.bounds.height - adView.bounds.height)
adView.sizeToFit()
adView.alpha = 0.001
adView.delegate = self
self.view .addSubview(adView)
self.canDisplayBannerAds = true
}
override func viewWillDisappear(animated: Bool) {
for view in self.view.subviews {
view.removeFromSuperview()
}
adView.hidden = true
adView.delegate = nil
adView.removeFromSuperview()
}
apparently I missed to add the "self.canDisplayBannerAds = true" and that fixed the problem for now