how to add logo to navigation bar in ios xamarin forms - xamarin

In my project I want to add logo to navigation bar. In android by using custom renderer I have placed the logo to navigation bar. But i don't know how to set the logo in navigation bar at globally in ios xamarin forms. Anyone give suggestion how to solve this.
I have tried like this,
NavigationPage.SetTilteIcon(this, "icon.png"); // But image is not display
Icon placement Reference Screenshot

You can try by setting the icon as:
Application.Current.MainPage = new NavigationPage(new FirstPageName() { Icon="icon.png"});
While navigating to another page you can write this as:
this.Navigation.PushAsync(new SecondPageName() { Icon="icon.png"});
Make sure you have the icon image in iOS project folder.

You can try custom renderer for a single page. But we can't set it at globally. Because in iOS every ViewController has a NavigationItem, we should set it when we need.
Here is some code about how to set NavigationItem in page renderer:
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
UIView leftView = new UIView(new CGRect(0, 0, 100, 40));
UIImageView imgView = new UIImageView(UIImage.FromBundle("icon.png"));
imgView.Frame = new CGRect(0, 5, 30, 30);
leftView.AddSubview(imgView);
UILabel label = new UILabel(new CGRect(30, 10, 70, 20));
label.Text = "page Title";
leftView.AddSubview(label);
//add event
UITapGestureRecognizer tap = new UITapGestureRecognizer((sender) =>
{
});
leftView.AddGestureRecognizer(tap);
leftItem = new UIBarButtonItem(leftView);
NavigationController.TopViewController.NavigationItem.SetLeftBarButtonItem(leftItem, true);
}
Please notice that put this image source in iOS's project.
Also if you do want to show this icon image in each page. Try to create a base page and construct this renderer for it. Then let your page inherit from it.

Related

Xamarin Forms: Change default page push animation on Android

In Xamarin Forms 5.0, I created a TabbedPage and nested NavigationPage instances as its children. I noticed that the page push animation does not slide from right to left unlike iOS. How can I change the default transition animation? Please note that I do not want to use a third-party library for animations. Ideally, I'd like to solve this problem with a custom renderer / effect.
private void SetMainPage()
{
var tabbedPage = new Xamarin.Forms.TabbedPage { BarBackgroundColor = Color.White };
Xamarin.Forms.PlatformConfiguration.AndroidSpecific.TabbedPage.SetToolbarPlacement(tabbedPage, ToolbarPlacement.Bottom);
tabbedPage.Children.Add(new NavigationPage(new AboutPage()) { BarBackgroundColor = Color.Black, Title = "Tab1", IconImageSource = "icon_about" });
tabbedPage.Children.Add(new NavigationPage(new AboutPage()) { BarBackgroundColor = Color.Black, Title = "Tab2", IconImageSource = "icon_feed" });
MainPage = tabbedPage;
}
Why don't you just use NavigationPage as the parent of your TabbedPage ? I am using XF 5.0.0.2012 and working with this way

How to Hide Tabbar Top Border color in IOS Xamarin.forms

I have Changed the Tabbar selected text color by using the below code. But I don't know how to hide the tabbar top border color in xamarin.forms ios.
UITabBar.Appearance.SelectedImageTintColor = UIColor.FromRGB(247, 148, 30);
Set this in AppDelegate , it works perfectly on my side.
UITabBar.Appearance.BackgroundImage = new UIImage();
UITabBar.Appearance.ShadowImage = new UIImage();

How to Hide Navigation bar back button title globally in xamarin.ios

I used this code in AppDelegate,
UIBarButtonItem.Appearance.SetBackButtonTitlePositionAdjustment (new UIOffset (-100, -60), UIBarMetrics.Default);
But it position is changed to bottom. I have attached the result screenshot.
Solution1 : set Title Color instead of Title Position
UITextAttributes attribute = new UITextAttributes {TextColor = UIColor.Clear };
UIBarButtonItem.Appearance.SetTitleTextAttributes(attribute, UIControlState.Normal);
Solution2 : set horizontal offset only.
UIBarButtonItem.Appearance.SetBackButtonTitlePositionAdjustment (new UIOffset (-100, 0), UIBarMetrics.Default);

how to apply max height and enable scroll to Xamarin.Forms.Editor

I wanted chat application type user interface in my app & i am targeting android and iOS.
I am using Xamarin.Forms.Editor for reply
Editor _replyEntry = new Editor
{
HorizontalOptions = LayoutOptions.FillAndExpand,
TextColor = Color.Black
}
in this case the editor height remains constant and allows scrolling and editor does not expands
Then i used InvalidateMeasure()
_replyEntry .TextChanged += (sender, e) => { this.InvalidateMeasure(); };
in this case editor expands as when the text requires more space but does not allow scroll inside editor and if user types long message then editor does not allows scroll and text goes behind the keyboard and not visible to user
Is there any way to enable scroll and give max height to edit either in xamarin.forms of by writing custom renderer
Thanks
Here is my code
public class abc : ContentPage
{
public abc()
{
Image attchment = new Image
{
Source = "attachment.png",
HorizontalOptions = LayoutOptions.Start
};
Editor _replyEntry = new Editor
{
HorizontalOptions = LayoutOptions.FillAndExpand,
TextColor = Color.Black,
};
Button _sendButton = new Button
{
Text = "Send",
TextColor = Color.Black,
BackgroundColor = Color.Transparent,
HorizontalOptions = LayoutOptions.End
};
StackLayout replyStack = new StackLayout
{
Orientation = StackOrientation.Horizontal,
Padding = new Thickness(10),
Spacing = 10,
VerticalOptions = LayoutOptions.End,
Children = { attchment, _replyEntry, _sendButton }
};
Content = replyStack;
}
}
It looks like you will have to use a custom renderer to achieve what you are wanting.
There is a post here that has pretty much the same thing with what you are trying to achieve for Android.
In that demo it has an expanding multi-line EditText control (android:singleLine="false"), with only vertical scrollbars (android:scrollbars="vertical"), whilst disabling the horizontal scrollbars (android:scrollHorizontally="false").
You need to ensure the Editor' parent is expanding, then the editor will automatically expand too. If you make an empty contentpage and add an Editor, the is will just expand. If you place it inside a stacklayout, the you need to ensure that the stacklayout is expanding.

How to add a click handler to content page in xamarin forms

I am new at Xamarin.Forms and are trying to add a click event to my content page. I want an event to start when the user clicks on the page, no matter where.
I've created similar functionality in a WinPhone app, where I could solve my problem with OnLeftMouseButtonDown which was available on PhoneApplicationPage, but I could not find a suitable counterpart in the ContentPage. Any suggestions?
In order to get this working you have to add a Layout to the ContentPage, as you will want to specify some content, and set the HorizontalOptions and VerticalOptions to LayoutOptions.FillAndExpand.
This is not enough though to handle the taps correctly though.
You also need to specify a BackgroundColor for the Layout. I set mine to Color.Transparent. If you try without specifying a Color it does not work.
You then have to attach a TapGestureRecognizer to the ContentPage in order to catch the clicks that are made.
Although this works well with Labels and Buttons in my test below, still receiving the TapGestures for WindowsPhone on both types, along with the Button click event firing - this does not fully work with Android - as the Button click will prevent the TapGesture event from firing.
The other alternative is to try putting an 'invisible' Grid over the top of everything. However the issue with this approach is that you will loose the Click event handler from firing with WindowsPhone and also loose the Click event handler from firing with Android. The good part though - is that you can detect a click anywhere, although not pass this on. It just depends what your trying to achieve at the end of the day.
StackLayout objStackLayout = new StackLayout()
{
Orientation = StackOrientation.Horizontal,
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
BackgroundColor = Color.Transparent
};
//
Label objLabel1 = new Label();
objLabel1.BackgroundColor = Color.Red;
objLabel1.Text = "label1";
objLabel1.HorizontalOptions = LayoutOptions.Start;
objLabel1.VerticalOptions = LayoutOptions.Start;
objLabel1.WidthRequest = 100;
objLabel1.HeightRequest = 300;
objStackLayout.Children.Add(objLabel1);
//
Label objLabel2 = new Label();
objLabel2.BackgroundColor = Color.Green;
objLabel2.Text = "label2";
objLabel2.Font = Font.OfSize("Arial", 48);
objLabel2.WidthRequest = 100;
objLabel2.HeightRequest = 300;
objStackLayout.Children.Add(objLabel2);
//
Button objButton1 = new Button();
objButton1.Text = "Click Me";
objButton1.WidthRequest = 300;
objButton1.HeightRequest = 300;
objStackLayout.Children.Add(objButton1);
//
this.Content = objStackLayout;
TapGestureRecognizer objTapGestureRecognizer1 = new TapGestureRecognizer();
objTapGestureRecognizer1.Tapped += ((o2, e2) =>
{
System.Diagnostics.Debug.WriteLine("Clicked!");
});
this.Content.GestureRecognizers.Add(objTapGestureRecognizer1);

Resources