簡體   English   中英

Xamarin表格IOS - MKUserTrackingButton位置

[英]Xamarin Forms IOS - MKUserTrackingButton position

我目前在我的Xamarin Forms中有一個自定義地圖渲染器,用於每個平台的本地地圖渲染器。

對於iOS,我正在嘗試添加跟蹤按鈕以回到當前位置。

我有代碼來創建按鈕:

   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);

但我需要將它移到右下角(見下圖)

在此輸入圖像描述

所以我只需要代碼行來移動MAP視圖中的按鈕:)

如果要使用Frame更改Control的位置。 你應該刪除button.TranslatesAutoresizingMaskIntoConstraints = false; 此代碼將禁用Frame,並使用autoLayout來放置控件。

您也可以嘗試使用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;

這也將在右下方顯示跟蹤按鈕。 請注意,這僅適用於iOS 11及更高版本,因此請務必同時檢查設備。

    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)
            });
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM