UISearchController with Google Maps using Swift - ios

I'm trying to create a screen where there is going to be a Google map covering the whole screen and a button on the top showing the selected address (where the marker is pointing).
And I want when the user clicks on that button to be presented with a UISearchBar where he can search for locations using the Google Places API.
I have been following the following tutorial:
https://www.youtube.com/watch?v=gRQUoHleCGM
Unfortunately, in my case the search bar is not appearing (although the background is dimmed when I press the button).
Here are some screenshots from before I press the button and after.
And this is the code of my view controller.
class ChooseLocationViewController: UIViewController, GMSMapViewDelegate, UISearchBarDelegate, UISearchControllerDelegate {
let geocoder = GMSGeocoder()
var mapView: GMSMapView = GMSMapView.mapWithFrame(CGRectZero, camera:GMSCameraPosition.cameraWithLatitude(51.515339, longitude: -0.141838, zoom: 16))
var addressButton: UIButton = UIButton()
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
let marker = UIImageView(frame: CGRect(x: 20, y: 20, width: 50, height: 50))
marker.image = UIImage(named: "default-marker")
marker.center.x = self.view.center.x
marker.center.y = self.view.center.y - (self.navigationController?.navigationBar.frame.size.height)!
addressButton = UIButton(frame: CGRect(x: 20, y: 20, width: self.view.frame.size.width - 25, height: 47))
addressButton.center.x = self.view.center.x
addressButton.backgroundColor = UIColor(red: 248/255, green: 248/255, blue: 248/255, alpha: 1.0)
addressButton.layer.borderColor = UIColor(red: 178/255, green: 178/255, blue: 178/255, alpha: 1.0).CGColor
addressButton.layer.borderWidth = 1.0
addressButton.layer.cornerRadius = 5
addressButton.setTitleColor(UIColor(red: 68/255, green: 68/255, blue: 68/255, alpha: 1.0), forState: .Normal)
addressButton.titleLabel?.font = UIFont.systemFontOfSize(13)
addressButton.addTarget(self, action: "showLocationSearch", forControlEvents: .TouchDown)
self.view = mapView
self.view.addSubview(marker)
self.view.addSubview(addressButton)
}
func showLocationSearch() {
let searchController = UISearchController(searchResultsController: nil)
searchController.delegate = self
searchController.searchBar.delegate = self
self.presentViewController(searchController, animated: true, completion: nil)
}
func willPresentSearchController(searchController: UISearchController) {
self.navigationController?.extendedLayoutIncludesOpaqueBars = true
}
func willDismissSearchController(searchController: UISearchController) {
self.navigationController?.extendedLayoutIncludesOpaqueBars = false
}
func mapView(mapView: GMSMapView, idleAtCameraPosition position: GMSCameraPosition) {
let coordinate = position.target
geocoder.reverseGeocodeCoordinate(coordinate) { response, error in
if let address = response?.firstResult() {
if let lines = address.lines {
if (lines.count > 0) {
self.addressButton.setTitle(lines[0], forState: .Normal)
}
}
}
}
}
}
Also, the reason I'm changing the extendedLayoutIncludesOpaqueBars is because I noticed that in other views where I have a search bar, this is necessary for it to be presented properly. In this case, I get exactly the same behavior (search bar is not showing) whether I do that or not.
What am I doing wrong? Did anyone have a similar issue?

Related

How to modify the width and height of UISearchBar in Swift?

I want to adjust the height and width of the searchBar in my app. I tried some solution from SO but there is something wrong in the adjustment of the width and height of the search bar. When I build and run the app, the image below is the look of my searchBar which is what I wanted to look like. Height is 70, width is 714.
But when I tapped the searchBar and start to type text, the size adjusted to smaller height. Please see 2nd image below.
Then when I tapped the cancel button it goes back to the default look of UISearchBar. Please see 3rd image below.
How can I modify width and height of searchBar permanently without experiencing auto adjustment while typing text or tapping the cancel button. Please take a look at my codes for your reference. Hope you can help me. Thank you.
private var searchBar: UISearchBar!
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
configureSearchBar()
searchController.searchBar.frame = CGRect(x: 15, y: 100, width: 714, height: 100)
}
override func viewDidAppear(_ animated: Bool) {
let searchTextField: UITextField = searchController.searchBar.subviews[0].subviews.last as! UITextField
searchTextField.layer.cornerRadius = 10
searchTextField.textAlignment = NSTextAlignment.left
searchTextField.placeholder = "Search by Name, Department or Employee Number"
searchTextField.rightViewMode = UITextFieldViewMode.always
}
//MARK: Function
func configureSearchBar() {
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
searchController.searchBar.textColor = UIColor.black
searchController.searchBar.placeholder = "Search by name, department or employee number"
searchController.searchBar.searchBarStyle = .prominent
searchController.searchBar.barTintColor = UIColor(red: 26/255.0, green: 99/255, blue: 42/255, alpha: 1.0)
searchController.searchBar.tintColor = UIColor.black
searchController.searchBar.backgroundColor = UIColor(red: 255/255.0, green: 255/255, blue: 255/255, alpha: 1.0)
searchController.searchBar.setImage(#imageLiteral(resourceName: "search"), for: .search, state: .normal)
let margins = searchController.searchBar.layoutMarginsGuide
searchController.searchBar.leadingAnchor.constraint(equalTo: margins.leadingAnchor, constant: 20).isActive = true
searchController.searchBar.isTranslucent = true
if #available(iOS 11.0, *) {
searchController.searchBar.heightAnchor.constraint(equalToConstant: 100).isActive = true
}
self.ParticipantTableView.tableHeaderView = searchController.searchBar
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
searchController.searchBar.layoutIfNeeded()
searchController.searchBar.layoutSubviews()
_ = searchController.searchBar.frame
let newheight: CGFloat = 70
let newWidth: CGFloat = 714
for subView in searchController.searchBar.subviews
{
for subsubView in subView.subviews
{
if let textField = subsubView as? UITextField
{
var currentTextFieldBounds = textField.bounds
currentTextFieldBounds.size.height = newheight
currentTextFieldBounds.size.width = newWidth
textField.bounds = currentTextFieldBounds
textField.borderStyle = UITextBorderStyle.roundedRect
}
}
}
}
extension ParticipantsViewController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
filterContentForSearchText(searchText: searchController.searchBar.text!)
}
}
extension UISearchBar {
var textColor: UIColor? {
get {
if let textField = self.value(forKey: "searchField") as? UITextField {
return textField.textColor
}else {
return nil
}
}
set (newValue) {
if let textField = self.value(forKey: "searchField") as? UITextField {
textField.textColor = newValue
textField.font = UIFont(name: "HelveticaNeue", size: 25.0)
}
}
}

can't set up UISwitch in Swift 4

Getting rustily back into iOS programming after some years away and am having trouble setting up a UISwitch. Here's my code:
#IBOutlet weak var firstConjugationVerbSwitch: UISwitch!
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(scrollView)
firstConjugationVerbSwitch.addTarget(self, action: #selector(changeText), for: .valueChanged)
}
#objc func changeText() {
print("changeText function running.")
}
The error I get when I toggle the switch is
[Latin_Substitution_Drills.VerbOptionsViewController firstConjugationVerbSwitch:]: unrecognized selector sent to instance 0x7ffcea817600
Any thoughts about what I'm doing wrong?
let customSwitch = UISwitch(frame:CGRect(x: 20, y: 20, width: 0, height: 0))
customSwitch.isOn = false
customSwitch.onTintColor = UIColor(red: 10/255, green: 105/255, blue: 122/255, alpha: 1)
//customSwitch.setOn(true, animated: true)
customSwitch.transform = CGAffineTransform(scaleX: 0.60, y: 0.60)
customSwitch.addTarget(self, action: #selector(switchTarget(sender:)), for: .valueChanged)
let switchButton = UIBarButtonItem(customView: customSwitch)
#objc func switchTarget(sender: UISwitch!)
{
if sender.isOn {
// do something ..
} else{
// do something ..
}
}

iOS Swift 3 - Remove Overlay UIView after Panel Tap

I am very new to swift and I am stuck with the task described in the title.
My Problem:
I am building a product page programmatically, consisting of a few simple details and an offer button. Tapping offer button brings up an overlay on the view with some other details. You click "Ok" and the overlay disappears.
All good except the overlay does not disappear!
What I have tried:
func hideOverlay(_ sender: UIButton) {
containerView.backgroundColor = UIColor.white
buttonView.backgroundColor = UIColor.white
for subview in overlayView.subviews {
subview.removeFromSuperview()
}
}
Function is called on tapping the button within the overlayView. I will include the showOverlay function(working).
func showOverlay(_ sender: UIButton) {
//Load overlay view
let overlayHeight : CGFloat = 500
let overlayWidth : CGFloat = 290
let overlayView = UIView(frame: CGRect(x: centreView(masterView: view.frame.width, subView: overlayWidth), y: 64 + centreView(masterView: (view.frame.height - 64), subView: overlayHeight), width: overlayWidth, height: overlayHeight))
overlayView.backgroundColor = UIColor.white
let overlayTitle = UILabel(frame: CGRect(x: 0, y: 0, width: overlayWidth, height: overlayHeight*1/5))
overlayTitle.text = "Offer Taken"
overlayTitle.font = UIFont.boldSystemFont(ofSize: 35)
overlayTitle.textAlignment = .center
overlayView.addSubview(overlayTitle)
let overlayButtonView = UIView(frame: CGRect(x: 0, y: 0 + (overlayHeight * 4/5), width: overlayWidth, height: overlayHeight * 1/5))
overlayButtonView.backgroundColor = UIColor.red
let buttonWidth : CGFloat = 100
let buttonHeight : CGFloat = 35
let overlayButton = UIButton(type: UIButtonType.system)
overlayButton.frame = CGRect(x: centreView(masterView: overlayWidth, subView: buttonWidth), y: overlayButtonView.frame.origin.y + centreView(masterView: overlayButtonView.frame.height, subView: buttonHeight), width: buttonWidth, height: buttonHeight)
overlayButton.backgroundColor = UIColor.blue
overlayButton.setTitle("OK",for: .normal)
overlayButton.setTitleColor(UIColor.white, for: .normal)
overlayButton.setTitle("Offer Taken", for: .highlighted)
overlayButton.setTitleColor(UIColor.white, for: .highlighted)
overlayButton.addTarget(self, action: #selector(self.hideOverlay(_:)), for: .touchUpInside)
overlayView.addSubview(overlayButtonView)
overlayView.addSubview(overlayButton)
containerView.backgroundColor = UIColor(colorLiteralRed: 0, green: 0, blue: 0, alpha: 0.5)
buttonView.backgroundColor = UIColor(colorLiteralRed: 0, green: 0, blue: 0, alpha: 0.5)
view.addSubview(overlayView)
}
I have tried
overlayView.removeFromSuperview()
after the for loop, but I fear that overlayView.subviews is not correctly filled with the views I expect.
I appreciate anyone taking the time to help me, even if a little closer to a solution.
In func showOverlay(_ sender: UIButton) {...} you are creating a local variable "overlayView":
let overlayView = UIView(frame: ...)
You then add that as a subview to view. All that is fine, except you do not keep a reference to "overlayView" .. the view remains but you have no reference to it.
Add a class-level variable, outside of any function blocks:
var overlayView: UIView!
Then, inside func showOverlay(_ sender: UIButton) {...}, instead of let overlayView = just assign it to the existing variable:
overlayView = UIView(frame: ...)
When you're ready to remove it:
func hideOverlay(_ sender: UIButton) {
overlayView.removeFromSuperview()
}
and you're done :)
Try the viewWithTag method described in a similar thread here:
Swift addsubview and remove it

UIView animation causing label to twitch

I have an IconView class that I use as a custom image for a Google Maps marker. All of the print statements show that the code is correctly executing. However, the "12:08" UILabel in circleView keeps on growing and shrinking (i.e. twitching). I can't figure out what the problem might be. I've tried manually setting the the font in the completion block, commenting out the adjustsFontSizeToFitWidth, changing the circleView to a UIButton.
import UIKit
class IconView: UIView {
var timeLabel: UILabel!
var circleView: UIView!
var clicked: Bool!
//constants
let circleViewWidth = 50.0
let circleViewHeight = 50.0
override init(frame:CGRect) {
super.init(frame : frame)
self.backgroundColor = UIColor(red: 47/255, green: 49/255, blue: 53/255, alpha: 0.0)
clicked = false
if !clicked {
//MAIN CIRCLE
print("init circle view")
circleView = UIView(frame: CGRect(x:0, y:0, width:circleViewWidth, height:circleViewHeight))
circleView.backgroundColor = UIColor(red: 47/255, green: 49/255, blue: 53/255, alpha: 1.0)
circleView.layer.cornerRadius = circleView.frame.size.height / 2.0
circleView.layer.masksToBounds = true
self.addSubview(circleView)
timeLabel = UILabel(frame: CGRect(x: 0, y: 0, width: circleViewWidth, height: circleViewHeight/3.0))
timeLabel.center = circleView.center
timeLabel.text = "12:08"
timeLabel.textAlignment = .center
timeLabel.textColor = .white
timeLabel.numberOfLines = 0
timeLabel.font = UIFont.systemFont(ofSize: 11)
timeLabel.font = UIFont.boldSystemFont(ofSize: 11)
timeLabel.adjustsFontSizeToFitWidth = true
circleView.addSubview(timeLabel)
}
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
let iconView = marker.iconView as! IconView
print("going to start animating")
if !iconView.clicked {
UIView.animate(withDuration: 0.2, animations: {
print("making this bigger now")
iconView.circleView.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
})
{ (finished:Bool) -> Void in
print("DONE")
iconView.clicked = true
}
}
return true
}

storyboard.instantiateViewController executes for about 3 seconds

I noticed that my app has a delay before presenting UIViewController, so I used print() to find out which line of code causing it and found up that let navigationVC = storyboard!.instantiateViewController(withIdentifier: "filterNavigationVC") as! UINavigationController executes for about 2 seconds. Why might it be like that? I've tried to remove everything except this one without even navigating and still it took that long.
#IBAction func filter(_ sender: AnyObject) {
let navigationVC = storyboard!.instantiateViewController(withIdentifier: "filterNavigationVC") as! UINavigationController
let destinationVC = navigationVC.topViewController as! FilterViewController
navigationVC.modalPresentationStyle = .overCurrentContext
destinationVC.backgroundColorValue = self.view.backgroundColor!
destinationVC.color = themeColor.light
destinationVC.delegate = self
destinationVC.billModel = self.price
destinationVC.applyedFilter = self.applyedFilter
self.present(navigationVC, animated: true, completion: nil)
}
Next UIViewController's viewDidLoad()
override func viewDidLoad() {
super.viewDidLoad()
customization()
}
fileprivate func customization(){
setupBillButtons(billModel.range["low"]!, medium: self.billModel.range["medium"]!, high: self.billModel.range["high"]!)
tempConfigButtons([self.lowBillButton, self.mediumBillButton, self.highBillButton])
if color == themeColor.light {
customizeLightTheme()
} else if color == themeColor.dark {
customizeDarkTheme()
}
setCharacterSpacingForLabels([typeLabel, kitchenLabel, avarageBill, metroLabel, tagsLabel], spacing: 2.79)
setSeparatorInsets([kitchenTableView, metroTableView, typeTableView], edge: UIEdgeInsetsMake(0, 20, 0, 20))
backgroundVoew.backgroundColor = backgroundColorValue
backgroundVoew.addBackroundBlur()
self.navigationController?.navigationBar.isHidden = true
}
fileprivate func customizeDarkTheme() {
comfirmButton.setImage(UIImage(named: "comfirmLight"), for: UIControlState())
dismissButton.setImage(UIImage(named: "closeLight"), for: UIControlState())
setColorsForTableViews([kitchenTableView, metroTableView, typeTableView], separatorColor: colorWithAlpha(whiteColor, alpha: 0.05), tintColor: colorWithAlpha(whiteColor, alpha: 0.5))
setColorForLabels([kitchenLabel, avarageBill, metroLabel, typeLabel, tagsLabel], color: colorWithAlpha(whiteColor, alpha: 0.50))
topName.textColor = whiteColor
UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent
self.view.backgroundColor = backgroundColorValue
bottomComfitmButton.tintColor = whiteColor
bottomComfitmButton.backgroundColor = colorWithAlpha(whiteColor, alpha: 0.15)
showAllTags.titleLabel!.addCharactersSpacing(-0.39, text: (self.showAllTags.titleLabel?.text)!)
showAllTags.setTitleColor(colorWithAlpha(whiteColor, alpha: 0.5), for: UIControlState())
showAllFilterImage.image = UIImage(named: "filterShowAllDark")
}
fileprivate func customizeLightTheme() {
comfirmButton.setImage(UIImage(named: "comfirmDark"), for: UIControlState())
dismissButton.setImage(UIImage(named: "closeDark"), for: UIControlState())
setColorForLabels([kitchenLabel, avarageBill, metroLabel, typeLabel, tagsLabel], color: colorWithAlpha(grayColor, alpha: 0.50))
topName.textColor = grayColor
setColorsForTableViews([kitchenTableView, metroTableView, typeTableView], separatorColor: colorWithAlpha(grayColor, alpha: 0.1), tintColor: colorWithAlpha(grayColor, alpha: 0.59))
UIApplication.shared.statusBarStyle = UIStatusBarStyle.default
bottomComfitmButton.tintColor = grayColor
bottomComfitmButton.backgroundColor = colorWithAlpha(whiteColor, alpha: 0.25)
showAllTags.titleLabel!.addCharactersSpacing(-0.39, text: (self.showAllTags.titleLabel?.text)!)
showAllTags.setTitleColor(colorWithAlpha(grayColor, alpha: 0.5), for: UIControlState())
showAllFilterImage.image = UIImage(named: "filterShowAllLight")
}
fileprivate func setupBillButtons(_ low: Bool, medium: Bool, high: Bool) {
lowBillButton.isSelected = low
mediumBillButton.isSelected = medium
highBillButton.isSelected = high
}
fileprivate func setSeparatorInsets(_ tableViews: [UITableView], edge: UIEdgeInsets) {
for tableView in tableViews {
tableView.separatorInset = edge
}
}
fileprivate func tempConfigButtons(_ buttons: [UIButton]) {
for button in buttons {
button.setTitleColor(whiteColor, for: .selected)
if self.color == themeColor.dark {
button.setBackgroundColor(colorWithAlpha(whiteColor, alpha: 0.50), forState: .selected)
button.setTitleColor(whiteColor, for: UIControlState())
DispatchQueue.main.async(execute: {
button.roundCorners([.topLeft, .topRight, .bottomLeft, .bottomRight], radius: 15, borderColor: self.colorWithAlpha(self.whiteColor, alpha: 0.50), borderWidth: 1)
})
} else {
button.setTitleColor(grayColor, for: UIControlState())
button.setBackgroundColor(colorWithAlpha(grayColor, alpha: 0.50), forState: .selected)
DispatchQueue.main.async(execute: {
button.roundCorners([.topLeft, .topRight, .bottomLeft, .bottomRight], radius: 15, borderColor: self.grayColor, borderWidth: 1)
})
}
}
}

Resources