How to remove the border or border colour of the SearchBar? - xamarin

I am new to Xamarin.forms. I am using a SearchBar followed by a BoxView.
The problem that I am facing is removing the border line of the SearchBar.
This border makes the UI appear as if there is a separator in between the SearchBar and the BoxView.
Any help in achieving this is much appreciated.
Thanks much :)

Think you might need a custom renderer for this like so:
using MonoTouch.UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly:ExportRenderer( typeof(NamespaceOfApp.MySearchBar), typeof(NamespaceOfApp.iOS.SearchBarWithNoBarRenderer_iOS))]
namespace NamespaceOfApp.iOS
{
public class SearchBarWithNoBarRenderer_iOS : SearchBarRenderer
{
protected override void OnElementChanged( ElementChangedEventArgs<SearchBar> args )
{
base.OnElementChanged( args );
UISearchBar bar = (UISearchBar)this.Control;
//set background to empty image
bar.SetBackgroundImage (new UIImage (), UIBarPosition.TopAttached, UIBarMetrics.Default);
}
}
}
If you need a hand with custom renderers look here

Effects was introduced in Forms v2 as the recommended way to extending the Forms base controls. https://developer.xamarin.com/guides/xamarin-forms/effects/
For example; this would be your iOS Effect code...
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly:ResolutionGroupName ("MyCompany")]
[assembly:ExportEffect (typeof(FocusEffect), "FocusEffect")]
namespace EffectsDemo.iOS
{
public class FocusEffect : PlatformEffect
{
protected override void OnAttached ()
{
// your code here
}
protected override void OnDetached ()
{
// your code here
}
protected override void OnElementPropertyChanged (PropertyChangedEventArgs args)
{
base.OnElementPropertyChanged (args);
// your code here
}
}
}
Custom Renderers were the only option in Forms v1.x. Custom Renderers are great for building controls from scratch but it was a bit hit and miss when extending the built-in controls.

Using StoryBoard:
Using code:
public override void ViewWillLayoutSubviews ()
{
base.ViewWillLayoutSubviews ();
sampleSearchBar.SearchBarStyle = UISearchBarStyle.Minimal;
sampleSearchBar.BarTintColor = UIColor.Clear;
}

Related

How can I create an spinning picker in Xamarin?

I am trying to get my picker to be an wheel/spinning type.
My Current Picker:
<Picker x:Name="AmountPicker" />
AmountPicker.ItemsSource = new List<string>() { "1", "2", "3","4","5","6","7","8" };
My Picker
You have a couple of options.
1. Use Custom Renderers which enables you to achieve the wheel/spinning type. There is a similar thread you could refer to: Apply styles on Picker items in Xamarin Forms.
1.1 Create the Custom Picker Control in Shared Project:
public class MyPicker : Picker
{
}
1.2 Creating the Custom Renderer on Android & iOS:
Android:
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(MyPicker), typeof(MyPickerRenderer))]
namespace App35.Droid
{
class MyPickerRenderer : PickerRenderer
{
public MyPickerRenderer(Context context) : base(context)
{
}
}
}
iOS:
[assembly: ExportRenderer(typeof(MyPicker), typeof(MyPickerRenderer))]
namespace App35.iOS
{
class MyPickerRenderer : PickerRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
{
base.OnElementChanged(e);
}
}
}
1.3 Consume it in XAML:
<local:MyPicker ItemsSource="{Binding MyListProperty}" ></local:MyPicker>
Output:
2. Use Xamarin.SfPicker. Firstly, install the package and then set SfPicker control namespace as xmlns:syncfusion="clr- namespace:Syncfusion.SfPicker.XForms;assembly=Syncfusion.SfPicker.XForms in XAML Content page. So you can consume it in XAML like below.For more you can refer to Getting Started with Xamarin Picker (SfPicker)
<syncfusion:SfPicker x:Name="picker" HeaderText="Choose Value" />
3. Use Wheel Picker for Xamarin Samples package, you can refer to Wheel Picker for Xamarin Samples for more details.

How can I create a Custom Renderer for a ContentView in Xamarin.Forms MacOS?

We're using Xamarin.Forms with MacOS, and have a custom view MyCustomView : Xamarin.Forms.ContentView, and I'm trying to create a custom view renderer for our view, but it's interfering with the rendered view.
Does anyone know how to create a view renderer in my platform project?
This is the code I've tried so far, looking at similar places:
[assembly: Xamarin.Forms.ExportRenderer(typeof(MyCustomView), typeof(MyCustomViewRenderer))]
namespace Mac.Renderers
{
public class MyCustomViewRenderer : ViewRenderer<Xamarin.Forms.ContentView, AppKit.NSView>
{
public MyCustomViewRenderer()
{
// My implementation
}
}
}
Event when the implementation is left blank, having this custom renderer is affecting the display of the ContentView, so I think this code must not be right - is there a way to do this?
You could fix your issue by changing your class to inherit from VisualElementRenderer<T> instead.
[assembly: Xamarin.Forms.ExportRenderer(typeof(MyCustomView), typeof(MyCustomViewRenderer))]
namespace Mac.Renderers
{
public class MyCustomViewRenderer : VisualElementRenderer<ContentView>
{
public MyCustomViewRenderer()
{
// My implementation
}
}
}
Page has a default Renderer "PageRenderer", do not understand why ContentView does not. It would be nice if there was a ContentViewRenderer.
Hope this helps.-

Xamarin Iconize IconTabbedPage Example

Can someone provide an example of how to use the IconTabbedPage in Iconize, preferably in Xaml? I have an IconTabbedPage with IconNavigation pages as children, all defined in Xaml. I then set the Icon property of the subpages by specifiying the font awesome name (“fa-home”). I tried to set the title as well, but neither of these will render the icon. I have search (a lot) for examples of the IconTabbedPage but couldn’t find any in Xaml. Additional bonus if you can provide an example of how to use the icons in a list cell context action.
Looking into #Niklas Code, you can create a tabbed page with a base class that inherits from IconTabbedPage , then your xaml will look like this.
<icon:IconTabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:icon="clr-namespace:Plugin.Iconize;assembly=Plugin.Iconize"
....
>
<icon:IconTabbedPage.Children>
<ContentPage Title="Build" Icon="md-build">
</ContentPage>
</icon:IconTabbedPage.Children>
I hope it will help somebody
I think you can take a look on Sample on GitHub
[\[assembly: XamlCompilation(XamlCompilationOptions.Compile)\]
namespace Iconize.FormsSample
{
public class App : Application
{
public App()
{
// The root page of your application
var tabbedPage = new IconTabbedPage { Title = "Iconize" };
foreach (var module in Plugin.Iconize.Iconize.Modules)
{
tabbedPage.Children.Add(new Page1
{
BindingContext = new ModuleWrapper(module),
Icon = module.Keys.FirstOrDefault()
});
}
MainPage = new IconNavigationPage(tabbedPage);
}
protected override void OnStart()
{
// Handle when your app starts
}
protected override void OnSleep()
{
// Handle when your app sleeps
}
protected override void OnResume()
{
// Handle when your app resumes
}
}
}][1]

Xamarin forms - Picker Renderer set 'spinner' font size

How do you change the font of the 'spinner' portion of the picker? I can change the display font doing the following
public class MyPickerRenderer : PickerRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
{
base.OnElementChanged(e);
if (Control != null)
{
**Control.Font = UIFont.SystemFontOfSize(8);**
}
}
}
Unfortunately this is not possible at least it's not subclassing the PickerRenderer defined in Xamarin.Forms for iOS.
The UIPickerView control that is displayed is marked as private for the renderer implementation, hence it will not be accesible from the subclass.
You could anyway do your own implementation of the Renderer and for this you could follow the implementation made by Xamarin.Forms (here you can see it) and do the modifications you need.
You will also need to subclass the UIPickerView class and override the ViewFor and there set the font size you want for the items..
Hope this helps.-

TailTruncation - Ellipsize the text of a picker control in Xamarin Forms

Is it possible to truncate long texts with ellipsis in a picker control. I have already created a custom renderer to set a fontsize and no border in order to achieve the following result.
Also tried to set Control.Ellipsize = TextUtils.TruncateAt.End; but nothing happens
[assembly: ExportRenderer(typeof(NoBorderPicker), typeof(CustomPicker))]
namespace Prj.Droid.Renderers
{
public class CustomPicker : PickerRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
{
base.OnElementChanged(e);
if (Control != null)
{
var customBG = new GradientDrawable();
customBG.SetColor(Android.Graphics.Color.Transparent);
customBG.SetCornerRadius(3);
Control.SetBackground(customBG);
Control.Ellipsize = TextUtils.TruncateAt.End;
var custdatepicker = (NoBorderPicker) this.Element;
this.Control.TextSize = (float)custdatepicker.FontSize;
}
}
}
}
Now, I could be sure that Control.SetSingleLine(true); will work.
if you are using a custom renderer can be the incorret inheritance.
Use Xamarin.Forms.Platform.Android.AppCompat.PickerRenderer not Xamarin.Forms.Platform.Android.PickerRenderer
Tks to https://www.damirscorner.com/blog/posts/20201204-CustomPickerRendererOnAndroid.html
Oddly enough, for me in the latest Xamarin Forms, on Android the Picker automatically truncates text, but on iOS the Picker becomes arbitrarily wide, covering up other UI elements.
The fix is to set the MinimumWidthRequest = 1, which for some reason re-enables text truncation. I have no idea why. Welcome to Xamarin.

Resources