美文网首页Xamarin
Forms里设置左边ToolbarItem(iOS)

Forms里设置左边ToolbarItem(iOS)

作者: ColeX | 来源:发表于2018-11-02 11:08 被阅读10次

    在Content中添加ToolbarItem 中默认都是显示在右边 ,现在需要第二个显示在左边,需要添加Custom Renderer 来实现

    [assembly: ExportRendererAttribute(typeof(ContentPage), typeof(CustomContentPageRenderer))]
    namespace AlertControllerDemo.iOS.Renderer
    {
    public class CustomContentPageRenderer : PageRenderer
    {
        public override void ViewWillAppear(bool animated)
        {
            base.ViewWillAppear(animated);
    
            // Did this, because Xamarin.Forms couldn't set HasBackButton correct.
            // ViewController.ParentViewController.NavigationItem.SetHidesBackButton(!((ChangePasswordScreen)this.Element).HasBackButton, false);
            if (NavigationController == null)
                return;
    
            var navigationItem = NavigationController.TopViewController.NavigationItem;
            var leftNativeButtons = (navigationItem.LeftBarButtonItems ?? new UIBarButtonItem[] { }).ToList();
            var rightNativeButtons = (navigationItem.RightBarButtonItems ?? new UIBarButtonItem[] { }).ToList();
    
            if (rightNativeButtons.Count > 1)
            {
                var nativeItem = rightNativeButtons.Last();
                rightNativeButtons.Remove(nativeItem);
                leftNativeButtons.Add(nativeItem);
            }
    
            navigationItem.RightBarButtonItems = rightNativeButtons.ToArray();
            navigationItem.LeftBarButtonItems = leftNativeButtons.ToArray();
        }
    }
    }
    

    效果:


    相关文章

      网友评论

        本文标题:Forms里设置左边ToolbarItem(iOS)

        本文链接:https://www.haomeiwen.com/subject/egmqxqtx.html