xamarin - 将搜索栏放置在顶部/导航栏中

标签 xamarin xamarin.forms

在 Forms 项目中,是否可以放置一个 SearchBar,使其显示在应用程序的顶部/导航栏中?我想要实现的是类似于 Android Youtube 应用程序的东西,只是跨平台的:

enter image description here

最佳答案

为此,您应该为您的页面编写一个渲染器

这是我的 iOS 实现(带有自定义“searchField”)

using CoreGraphics;
using Foundation;
using MyControls;
using MyRenderer;
using UIKit;
using Xamarin.Forms;

[assembly: ExportRenderer(typeof(MySearchContentPage), typeof(MySearchContentPageRenderer))]

namespace IOS.Renderer
{
    using Xamarin.Forms.Platform.iOS;

    public class MySearchContentPageRenderer : PageRenderer
    {
        public override void ViewWillAppear(bool animated)
        {
            base.ViewWillAppear(animated);

            SetSearchToolbar();
        }

        public override void WillMoveToParentViewController(UIKit.UIViewController parent)
        {
            base.WillMoveToParentViewController(parent);

            if (parent != null)
            {
                parent.NavigationItem.RightBarButtonItem = NavigationItem.RightBarButtonItem;
                parent.NavigationItem.TitleView = NavigationItem.TitleView;
            }
        }

        private void SetSearchToolbar()
        {
            var element = Element as MySearchContentPage;
            if (element == null)
            {
                return;
            }

            var width = NavigationController.NavigationBar.Frame.Width;
            var height = NavigationController.NavigationBar.Frame.Height;
            var searchBar = new UIStackView(new CGRect(0, 0, width * 0.85, height));

            searchBar.Alignment = UIStackViewAlignment.Center;
            searchBar.Axis = UILayoutConstraintAxis.Horizontal;
            searchBar.Spacing = 3;

            var searchTextField = new MyUITextField();
            searchTextField.BackgroundColor = UIColor.FromRGB(239, 239, 239);
            NSAttributedString strAttr = new NSAttributedString("Search", foregroundColor: UIColor.FromRGB(146, 146, 146));
            searchTextField.AttributedPlaceholder = strAttr;
            searchTextField.SizeToFit();

            // Delete button
            UIButton textDeleteButton = new UIButton(new CGRect(0, 0, searchTextField.Frame.Size.Height + 5, searchTextField.Frame.Height));
            textDeleteButton.Font = UIFont.FromName("FontAwesome", 18f);
            textDeleteButton.BackgroundColor = UIColor.Clear;
            textDeleteButton.SetTitleColor(UIColor.FromRGB(146, 146, 146), UIControlState.Normal);
            textDeleteButton.SetTitle("\uf057", UIControlState.Normal);
            textDeleteButton.TouchUpInside += (sender, e) => 
            {
                searchTextField.Text = string.Empty;
            };

            searchTextField.RightView = textDeleteButton;
            searchTextField.RightViewMode = UITextFieldViewMode.Always;

            // Border
            searchTextField.BorderStyle = UITextBorderStyle.RoundedRect;
            searchTextField.Layer.BorderColor = UIColor.FromRGB(239, 239, 239).CGColor;
            searchTextField.Layer.BorderWidth = 1;
            searchTextField.Layer.CornerRadius = 5;
            searchTextField.EditingChanged += (sender, e) => 
            { 
                element.SetValue(MySearchContentPage.SearchTextProperty, searchTextField.Text);
            };

            searchBar.AddArrangedSubview(searchTextField);

            var searchbarButtonItem = new UIBarButtonItem(searchBar);
            NavigationItem.SetRightBarButtonItem(searchbarButtonItem, true);

            NavigationItem.TitleView = new UIView();

            if (ParentViewController != null)
            {
                ParentViewController.NavigationItem.RightBarButtonItem = NavigationItem.RightBarButtonItem;
                ParentViewController.NavigationItem.TitleView = NavigationItem.TitleView;
            }
        }
    }
}

此外,还有一些讨论:How to include view in NavigationBar of Xamarin Forms?

enter image description here

我希望您能理解主要思想。

关于xamarin - 将搜索栏放置在顶部/导航栏中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29047041/

相关文章:

xamarin - 如何通过传入类型参数来自定义 Xamarin 模板?

ios - Visual Studio 2015/Xamarin,调试不工作 : an error occurred while executing MTouch

c# - Xamarin Sqlite-Net Insert(对象引用未设置为对象的实例)

ios - Xamarin Forms : ImageSource. FromUri 不显示

xamarin - ListView 在 xamarin.forms 中水平和垂直滚动

c# - Xamarin iOS C# 从 UIWebView 在 Safari 中打开链接

xaml - Xamarin 表格 : CarouselView for static content

c# - 对象处置异常 : The CancellationTokenSource has been disposed

c# - 创建 Xamarin.Forms 跨平台应用程序时出现 "This project requires a Visual Studio update to load"错误

android - Xamarin.Forms(Android) 人脸检测器