在顶部/导航栏中放置一个 SearchBar [英] Placing a SearchBar in the top/navigation bar
本文介绍了在顶部/导航栏中放置一个 SearchBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 Forms 项目中,是否可以放置 SearchBar 使其出现在应用程序的顶部/导航栏中?我想要实现的是与 Android Youtube 应用程序类似的东西,只是跨平台:
解决方案
为此,您应该为您的页面编写渲染器
有我的 iOS 实现(带有自定义searchField")
使用CoreGraphics;使用基金会;使用我的控件;使用 MyRenderer;使用 UIKit;使用 Xamarin.Forms;[组装:ExportRenderer(typeof(MySearchContentPage), typeof(MySearchContentPageRenderer))]命名空间 IOS.Renderer{使用 Xamarin.Forms.Platform.iOS;公共类 MySearchContentPageRenderer : PageRenderer{公共覆盖无效 ViewWillAppear(布尔动画){base.ViewWillAppear(动画);SetSearchToolbar();}公共覆盖无效 WillMoveToParentViewController(UIKit.UIViewController parent){base.WillMoveToParentViewController(parent);如果(父母!= null){parent.NavigationItem.RightBarButtonItem = NavigationItem.RightBarButtonItem;parent.NavigationItem.TitleView = NavigationItem.TitleView;}}私有无效 SetSearchToolbar(){var element = Element as MySearchContentPage;如果(元素==空){返回;}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();//删除按钮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;//边界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();如果(ParentViewController != null){ParentViewController.NavigationItem.RightBarButtonItem = NavigationItem.RightBarButtonItem;ParentViewController.NavigationItem.TitleView = NavigationItem.TitleView;}}}}
另外,还有一些讨论:
我希望你理解了主要思想.
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:
解决方案
To do this, you should write a renderer for your Page
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;
}
}
}
}
Also, there is some discussion:How to include view in NavigationBar of Xamarin Forms?
I hope, you understood the main idea.
这篇关于在顶部/导航栏中放置一个 SearchBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文