SearchController SearchBar width issue iOS11 - ios

I am trying to add SearchController SearchBar programmatically in one of UIVIew as subView but initially it working properly up to iOS 10 but getting issue in iOS11 width getting increased get displaced.
I tried all possible solutions, Created Custom class init SearchBar frame with CGRect.zero & even hide cancel button. Still i does not understanding where I am doing wrong.
Initial Screen:
Issue is here when i clicked on SearchBar
Irony is its not working even i am trying to reset frame width to 100.0f by calling method viewWillLayoutSubviews from searchBarShouldBeginEditing
func setUpSearchBar(){
self.automaticallyAdjustsScrollViewInsets = false
resultsViewController = GMSAutocompleteResultsViewController()
resultsViewController?.delegate = self as GMSAutocompleteResultsViewControllerDelegate
searchController = CustomSearchController(searchResultsController: resultsViewController)
searchController?.searchResultsUpdater = resultsViewController
//resultsViewController?.view.frame = searchAndButtonView.bounds
//let subView = UIView(frame: CGRect(x: 0, y: 65.0, width: 350.0, height: 45.0))
searchController?.searchBar.frame = CGRect(x: 0, y: 0, width: searchView.frame.size.width, height: searchView.frame.size.height)
searchController?.searchBar.delegate = self
searchView.addSubview((searchController?.searchBar)!)
searchView.translatesAutoresizingMaskIntoConstraints = false
searchController?.searchBar.sizeToFit()
searchController?.hidesNavigationBarDuringPresentation = false
searchController?.searchBar.searchBarStyle = .minimal
definesPresentationContext = false
self.extendedLayoutIncludesOpaqueBars = true
self.edgesForExtendedLayout = .top
}
CustomSearchController.swift
class CustomSearchController: UISearchController,UISearchBarDelegate{
let noCancelButtonSearchBar = NoCancelButtonSearchBar()
lazy var _searchBar: NoCancelButtonSearchBar = {
[unowned self] in
let customSearchBar = NoCancelButtonSearchBar(frame: CGRect.zero)
return customSearchBar
}()
override var searchBar: UISearchBar {
get {
return _searchBar
}
}
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.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
class NoCancelButtonSearchBar: UISearchBar {
override func setShowsCancelButton(_ showsCancelButton: Bool, animated: Bool) { /*
void */ }
}

After lot of attempts i resolved issue by following way,
I removed customised CustomSearchController class & used simple UISearchController .
func setUpSearchBar(){
self.automaticallyAdjustsScrollViewInsets = false
resultsViewController = GMSAutocompleteResultsViewController()
resultsViewController?.delegate = self as GMSAutocompleteResultsViewControllerDelegate
searchController = UISearchController(searchResultsController: resultsViewController)
searchController?.searchResultsUpdater = resultsViewController
searchController?.searchBar.frame = CGRect(x: 0, y: 0, width: searchView.frame.size.width, height: searchView.frame.size.height)
let searchBarContainer = SearchBarContainerView(customSearchBar: searchController?.searchBar)
searchBarContainer.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 44)
searchController?.searchBar.delegate = self
searchView.addSubview(searchBarContainer)
searchView.translatesAutoresizingMaskIntoConstraints = false
searchController?.searchBar.sizeToFit()
searchController?.hidesNavigationBarDuringPresentation = false
searchController?.searchBar.searchBarStyle = .minimal
definesPresentationContext = false
self.extendedLayoutIncludesOpaqueBars = true
self.edgesForExtendedLayout = .top
}
And add SearchBarContainerView class,
class SearchBarContainerView: UIView {
let searchBar: UISearchBar
init(customSearchBar: UISearchBar) {
searchBar = customSearchBar
super.init(frame: CGRect.zero)
addSubview(searchBar)
}
override convenience init(frame: CGRect) {
self.init(customSearchBar: UISearchBar())
self.frame = frame
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
searchBar.frame = bounds
}
}
Following is working screenshot:

Try this code. it worked for me. I was also facing the same issue.
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
searchbar.invalidateIntrinsicContentSize()
}

Related

largeTitle of navigationBar collapse after refreshControl.endRefreshing swift

Encountered a problem: largeTitle of navigation bar collapsing to small title ~once per 20 attempts after refreshControl.endRefreshing().
tableView is first subview of subviews hierarchy, and top constraint is to superview (not to safeArea)
refreshControl added to tableView.refreshControl, not as subview of tableView
was trying call navigationBar.sizeToFit() after endRefreshing - not success.
Controller code:
override public func viewDidLoad() {
super.viewDidLoad()
definesPresentationContext = true
title = L10n.Trainers.trainers.uppercased()
navigationController?.navigationBar.layoutMargins = .init(top: 0, left: 16, bottom: 0, right: 16)
configureSubviews()
makeConstraints()
configureBindings()
configureActions()
setupTableView()
setupResultsTableView()
}
override public func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.navigationBar.prefersLargeTitles = true
navigationController?.navigationItem.largeTitleDisplayMode = .always
navigationController?.navigationBar.sizeToFit()
}
override public func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
navigationController?.navigationItem.largeTitleDisplayMode = .automatic
}
override public func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
vm.onViewDidAppear()
mainView.tableView.refreshControl = mainView.refreshControl
}
override public func loadView() {
view = mainView
}
Refresh-logic code:
vm.isLoading.drive { [weak self] loading in
guard let self = self else { return }
if self.mainView.refreshControl.isRefreshing {
if !loading {
self.mainView.tableView.refreshControl?.endRefreshing()
}
} else {
if loading {
var style = LoaderParameters.blue
style.installConstraints = false
self.mainView.tableView.startLoading(with: style)
self.mainView.tableView.existedLoaderView?.centerX().centerY(-40)
} else {
self.mainView.tableView.stopLoadingProgress()
}
}
}.store(in: &subscriptions)
View code:
final class TrainersView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
configureSubviews()
makeConstraints()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
private(set) lazy var refreshControl = UIRefreshControl().configureWithAutoLayout {
$0.tintColor = Asset.mainGrey.color
}
private(set) lazy var topView: UIView = {
let view = UIView()
view.backgroundColor = .clear
return view
}()
private(set) lazy var tableView = UITableView().configureWithAutoLayout {
$0.register(TrainerCell.self)
$0.tableFooterView = UIView()
$0.showsVerticalScrollIndicator = false
$0.separatorStyle = .none
$0.backgroundColor = Asset.mainWhite.color
}
private func configureSubviews() {
addSubview(tableView)
topView.addSubview(segmentedControl)
tableView.tableHeaderView = topView
tableView.tableHeaderView?.frame.size.height = 60
tableView.tableFooterView?.frame.size.height = 40
tableView.reloadData()
}
private func makeConstraints() {
tableView.pinToSuperview()
segmentedControl.centerX()
segmentedControl.bottomAnchor.constraint(equalTo: topView.bottomAnchor, constant: -8).priority(999).activate()
segmentedControl.widthAnchor ~ widthAnchor - 16 * 2
}
}

rightBarButtonItem appearing in the middle

The rightbarbuttonitem is not appearing on the right side of the navigation bar. I want the navigation bar to look similar to the one in the "App Store"
I have tried doing this in the storyboard and in the code, setting the image content mode, clipping to bounds, and giving it a frame.
I have also been looking at solutions online and none of them have worked for me. Any help or suggestions would be appreciated, thanks.
Here are some screenshots:
import UIKit
class KYSearchBarController: UISearchController {
override init(searchResultsController: UIViewController?) {
super.init(searchResultsController: searchResultsController)
}
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
// Call in view did appear
func CustomizeSearchBar() {
// Changing color of text in textfield.
let textfieldInsideBar = self.searchBar.value(forKey: "searchField") as? UITextField
textfieldInsideBar?.textColor = .darkGray
// Chaning placeholder
let textfieldLbl = textfieldInsideBar?.value(forKey: "placeholderLabel") as? UILabel
textfieldLbl?.textColor = .darkGray
textfieldLbl?.textAlignment = .center
// Icon customization
let glassIcon = textfieldInsideBar?.leftView as? UIImageView
glassIcon?.image = #imageLiteral(resourceName: "icon")
glassIcon?.image?.withRenderingMode(.alwaysTemplate)
glassIcon?.tintColor = .darkGray
// Centering textfield text
textfieldInsideBar?.textAlignment = .center
let clearButton = textfieldInsideBar?.value(forKey: "clearButton") as! UIButton
clearButton.setImage(UIImage(named: "icon1"), for: .normal)
clearButton.tintColor = .darkGray
}
}
extension UIView {
func MakeRound() {
self.layer.cornerRadius = self.frame.width / 5.0
}
}
class ViewController: UIViewController, UISearchBarDelegate {
let searchController = KYSearchBarController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
let userimage = UIImageView(image: UIImage(named: "person1"))
userimage.frame = CGRect(x: 60, y: 0, width: 50, height: 50)
userimage.clipsToBounds = true
userimage.layer.masksToBounds = true
userimage.contentMode = .scaleAspectFit
userimage.MakeRound()
let rightBarButton = UIBarButtonItem(customView: userimage)
navigationItem.rightBarButtonItem = rightBarButton
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
searchController.CustomizeSearchBar()
}
}
Add the userimage property to make it accessible inside the ViewController.
class ViewController: UIViewController, UISearchBarDelegate {
let searchController = KYSearchBarController(searchResultsController: nil)
let userimage = UIImageView(image: UIImage(named: "person1"))
}
Add the makeRound() function call to viewWillLayoutSubviews().
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
userimage.makeRound()
}
Update the makeRound() function to make a circle.
extension UIView {
func makeRound() {
self.layer.cornerRadius = self.frame.width / 2.0
}
}
Add a method to add the necessary constraints.
func setupConstraints() {
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
guard let navigationBar = self.navigationController?.navigationBar else { return }
navigationBar.addSubview(userimage)
userimage.clipsToBounds = true
userimage.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
userimage.rightAnchor.constraint(equalTo: navigationBar.rightAnchor, constant: -16),
userimage.bottomAnchor.constraint(equalTo: navigationBar.bottomAnchor, constant: -12),
userimage.heightAnchor.constraint(equalToConstant: 40),
userimage.widthAnchor.constraint(equalTo: userimage.heightAnchor)
])
}
Setup a gesture recognizer for the UIImageView and implementation for it.
func setUpGestureRecognizer() {
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(profile))
userimage.isUserInteractionEnabled = true
userimage.addGestureRecognizer(tapGestureRecognizer)
}
#objc func profile() {
// Your implementation
}
Update viewDidLoad() with the method call.
override func viewDidLoad() {
super.viewDidLoad()
// Setup constraints
setupConstraints()
setUpGestureRecognizer()
}
I ran into the same issue when I was using a very large image for my UIBarButtonItem.
Once I resized my image to a smaller size, it was appropriately placed at the right hand side of the navigation bar. It looks like you are having the same issue.
Alternatively, since starting from iOS 11 navigation bar uses autolayout, replacing the line
userimage.frame = CGRect(x: 60, y: 0, width: 50, height: 50)
with the below should also do the trick:
userimage.widthAnchor.constraint(equalToConstant: 50).isActive = true
userimage.heightAnchor.constraint(equalToConstant: 50).isActive = true

Creating UIView subclass in Xcode Playgrounds Programmatically

UIViewController and UIView class in Xcode Playgrounds
I am trying to figure out how to create a UIViewController in Xcode Playgrounds and attaching a UIView subclass to it.
I am quite new to swift and coding so am having some trouble.
class myView : UIView {
var label : UIButton!
var topHeader : UIImageView!
var nameText : UITextField!
var password : UITextField!
override init(frame: CGRect) {
super.init(frame: CGRect(x: 10, y: 10, width: 350, height: 450))
self.backgroundColor = UIColor.white
self.layer.borderColor = UIColor.black.cgColor
self.layer.borderWidth = 0.5
//place matters what gets loaded in first.
header("")
loginButton()
nameyText()
nameyText2()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
convenience init() {
self.init(frame: CGRect.zero)
}
func header(_ imageInsert: String){
let topHeader = UIImageView()
topHeader.frame = CGRect(x: -1, y: -1, width: 400, height: 125)
topHeader.backgroundColor = UIColor.white
topHeader.layer.borderWidth = 0.5
topHeader.translatesAutoresizingMaskIntoConstraints = true
topHeader.image = UIImage(named: imageInsert)
self.addSubview(topHeader)
}
func loginButton (){
//create properties for button
let label = UIButton(type: UIButtonType.roundedRect)
label.backgroundColor = .white
label.frame = CGRect(x: 135, y: 315, width: 75, height: 30)
label.setTitle("Login", for: UIControlState.normal)
label.layer.borderWidth = 0.5
label.layer.borderColor = UIColor.blue.cgColor
label.layer.cornerRadius = 4
self.addSubview(label)
}
func nameyText () {
let nameText = UITextField()
nameText.frame = CGRect(x: 45, y: 200, width: 250, height: 30)
nameText.backgroundColor = .white
nameText.borderStyle = .roundedRect
nameText.layer.borderColor = UIColor.lightGray.cgColor
nameText.placeholder = "Username"
nameText.keyboardAppearance = .default
self.addSubview(nameText)
}
func nameyText2 () {
let password = UITextField()
password.frame = CGRect(x: 45, y: 255, width: 250, height: 30)
password.backgroundColor = .white
password.borderStyle = .roundedRect
password.layer.borderColor = UIColor.lightGray.cgColor
password.placeholder = "Password"
password.keyboardAppearance = .default
password.isSecureTextEntry = true
self.addSubview(password)
}
}
This is the UIView subclass i created which looks like this...
UIView
I then created my main UIViewController class to add it to...
import UIKit
import PlaygroundSupport
public class ViewController : UIViewController, UITextFieldDelegate {
public override func viewDidLoad() {
super.viewDidLoad()
}
override public func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
public override func loadView() {
//trying to make myView the root view for This ViewController
self.view = myView()
}
}
PlaygroundPage.current.liveView = ViewController()
However, when i add my UIView subclass to the UIViewController it looks like this...UIViewController
It's a different size and changing my UIView frame size does nothing.
Would like some help understanding why it does this, thanks.
You need to set the .preferredContentSize of the view controller. You can do this either explicitly, or by overriding it:
public class ViewController : UIViewController, UITextFieldDelegate {
override public var preferredContentSize: CGSize {
get { return self.view.bounds.size }
set { super.preferredContentSize = newValue }
}
public override func viewDidLoad() {
super.viewDidLoad()
}
override public func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
public override func loadView() {
//trying to make myView the root view for This ViewController
self.view = myView()
}
}

UIView of TableView can't scroll or tap

What I did was when the search controller is clicked, it shows a View with a tableView in it. (like Instagram) . It shows the tableView but it cannot interact with it.
I was doing some research since people had come across this before here. Here are the things I've tried:
Bring subView to the front
have set tableView.isUserInteractionEnabled = true -> Have also ran on iPhone
Have set tableViewHeight to ScreenHeight
But the tableView still doesn't want to scroll/click!
Here's the relevant code that I have if it helps,
Controller with search bar and collection views:
class UserSearchController: UICollectionViewController, UICollectionViewDelegateFlowLayout,UISearchBarDelegate, UISearchDisplayDelegate {
let cellId = "cellId"
let searchBar: UISearchBar = {
let sb = UISearchBar()
sb.placeholder = "Search"
return sb
}()
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
searchBar.setShowsCancelButton(true, animated: true)
tableView.isHidden = false
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.setShowsCancelButton(true, animated: true)
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
searchBar.setShowsCancelButton(false, animated: true)
searchBar.text = ""
tableView.isHidden = true
}
let tableView: UIView = {
let tv = SearchUsersTv()
tv.isUserInteractionEnabled = true
tv.bringSubview(toFront: tv)
tv.clipsToBounds = false
return tv
}()
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.register(UserProfileVideoCell.self, forCellWithReuseIdentifier: cellId)
view.addSubview(tableView)
tableView.isHidden = true
}
Here's the code for the relevant code for the tableView (SearchUsersTv):
class SearchUsersTv: UIView, UITableViewDelegate, UITableViewDataSource {
let cellId = "cellId"
var tableView = UITableView()
override init(frame: CGRect){
super.init(frame: frame)
setupTv()
}
func setupTv() {
let screenHeight = UIScreen.main.bounds.height
let screenWidth = UIScreen.main.bounds.width
tableView.delegate = self
tableView.dataSource = self
tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
tableView.isUserInteractionEnabled = true
tableView = UITableView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight))
self.addSubview(tableView)
bringSubview(toFront: tableView)
}
Problem to fix: Make tableView scroll and click
Thank you in advanced!
Your problems is that you are initialising your custom class in wrongly way, you need call SearchUsersTv(frame: instead of SearchUsersTv() for initialisation because all your tableView setup happen on setupTv() which is called in SearchUsersTv(frame: initialisation only
replace your tableView inline creation by this
let tableView: UIView = {
let screenHeight = UIScreen.main.bounds.height
let screenWidth = UIScreen.main.bounds.width
let tv = SearchUsersTv(frame: CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight))
tv.isUserInteractionEnabled = true
tv.bringSubview(toFront: tv)
tv.clipsToBounds = false
tv.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cellId")
return tv
}()

adding searchBar to navigationBar in swift

i'm trying to add a searchBar to the navigationBar. i've researched a bit and this is what i've done so far:
The problem is nothing appear to the navigationBar using this code.
searchBar = UISearchBar(frame: CGRectMake(0, 0, 320, 44))
searchBar?.delegate = self
searchBar?.showsCancelButton = true
searchController = UISearchDisplayController()
searchController?.delegate = self
searchController?.searchResultsDelegate = self
searchController?.displaysSearchBarInNavigationBar = true
Try this code that worked for me:
lazy var searchBars:UISearchBar = UISearchBar(frame: CGRectMake(0, 0, 200, 20))
override func viewDidLoad() {
super.viewDidLoad()
var leftNavBarButton = UIBarButtonItem(customView: searchBars)
self.navigationItem.leftBarButtonItem = leftNavBarButton
}
Try this
lazy var searchBar = UISearchBar(frame: .zero)
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.titleView = searchBar}
You can use this
private let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
self.navigationItem.searchController = searchController
self.navigationItem.hidesSearchBarWhenScrolling = false
}

Resources