royvano royvano - 1 year ago 87
Swift Question

How to let a textfield hover/float above a mapView

I'm making an app which uses MapView, now I've positioned the textfield above the mapview, like on the image.Image But I really want to fullscreen the mapview and let a textfield float/hover above it. Does anybody know how I can make something like that? I have searched a lot on the internet but couldn't find any clue.. Hope someone can get me starting.

Answer Source

Right, presumably your map view is pinned on all four sides to its superview? Setup your constraints as follows, not how the mapview is not pinned to the text field, but the superview.

To add the drop shadow, set the following properties on your UITextField:

textField.layer.shadowOpacity = 1.0
textField.layer.shadowRadius = 0.0
textField.layer.shadowColor = UIColor.blackColor().CGColor
textField.layer.shadowOffset = CGSizeMake(0.0, -1.0)

Adjust the settings until you are happy with the look of the shadow.

To dismiss the keyboard when the user pans around the map is very simple. You just need to implement the delegate. Edit your class to look a bit like this:

class MapViewController: UIViewController, MKMapViewDelegate {
    @IBOutlet var mapView: MKMapView!
    @IBOutlet var textField: UITextField!

    override func viewDidLoad() {
        mapView.delegate = self

    func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download