2
votes

I currently have a Custom map renderer in my Xamarin Forms that use for each platform the native Map renderer.

For iOS, I'm trying to add a tracking button to come back to current position.

I have the code to create the button :

   var button = MKUserTrackingButton.FromMapView(Map);
            button.Layer.BackgroundColor = UIColor.White.CGColor;
            button.Layer.BorderColor = UIColor.FromRGB(211, 211, 211).CGColor;
            button.Layer.BorderWidth = 1;
            button.Layer.CornerRadius = 5;
            button.TranslatesAutoresizingMaskIntoConstraints = false;
            Map.AddSubview(button);

But I need to move it to the bottom right corner ( see image below )

enter image description here

So I just need the line of code to move the button in the MAP View :)

2
set the button's Frame to specify a location relative to the parent - Jason
This code button.Frame = new RectangleF(100, 100, 52, 44); doesn't work - OrcusZ

2 Answers

2
votes

If you want to use Frame to change a Control's position. You should delete button.TranslatesAutoresizingMaskIntoConstraints = false;. This code will disable the Frame, and use autoLayout to place your controls.

Also you can try to use autoLayout:

button.TopAnchor.ConstraintEqualTo(Map.TopAnchor, 100).Active = true;
button.LeadingAnchor.ConstraintEqualTo(Map.LeadingAnchor, 100).Active = true;
button.WidthAnchor.ConstraintEqualTo(52).Active = true;
button.HeightAnchor.ConstraintEqualTo(44).Active = true;
1
votes

This will also give the tracking button on the bottom right. Note that this only works for iOS 11 and above, so be sure to put a device check in there also.

    if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0))
        {
            var button = MKUserTrackingButton.FromMapView(map);
            button.Layer.BackgroundColor = UIColor.White.CGColor;
            button.Layer.BorderColor = UIColor.FromRGB(0, 0, 127).CGColor;
            button.Layer.BorderColor = UIColor.White.CGColor;
            button.Layer.BorderWidth = 1;
            button.Layer.CornerRadius = 5;
            button.TranslatesAutoresizingMaskIntoConstraints = false;
            View.AddSubview(button); 
            NSLayoutConstraint.ActivateConstraints(new NSLayoutConstraint[]{
                button.BottomAnchor.ConstraintEqualTo(View.BottomAnchor, -10),
                button.TrailingAnchor.ConstraintEqualTo(View.TrailingAnchor, -10)
            });
        }