在顶部/导航栏中放置一个SearchBar [英] Placing a SearchBar in the top/navigation bar

查看:105
本文介绍了在顶部/导航栏中放置一个SearchBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In a Forms project, is it possible to place a SearchBar such that it appears in the top/navigation bar of the app? What I want to achieve is something along the lines of the Android Youtube app, just cross-platform:

推荐答案

为此,您应该为Page编写一个渲染器.

To do this, you should write a renderer for your Page

有我的iOS实现(带有自定义"searchField")

There is my implementation for iOS (with custom '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;
            }
        }
    }
}

也有一些讨论:如何包含视图在Xamarin表单的NavigationBar中?

我希望您了解主要思想.

I hope, you understood the main idea.

这篇关于在顶部/导航栏中放置一个SearchBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆