I'm trying to add a search bar inside the navigation bar, here's my code:
class ViewController: UIViewController {
var searchController : UISearchController!
override func viewDidLoad() {
super.viewDidLoad()
searchController = UISearchController(searchResultsController:nil)
searchController.hidesNavigationBarDuringPresentation = false
navigationItem.searchController = searchController
definesPresentationContext = true
}
}
It seems to work fine, but I can't find a way to reduce the navigation bar height. I don't need the extra space at the top.
Here's what I'm trying to achieve (default Messages search bar):
Related
I am attempting to change the size of the back arrow in the navigation bar.
Using information from this article, I have added the following code:
protected override Task<bool> OnPushAsync(Page view, bool animated)
{
var result = base.OnPushAsync(view, animated);
var activity = (Activity) Context;
var toolbar = activity.FindViewById<Toolbar>(Resource.Id.toolbar);
if (toolbar != null)
{
if (toolbar.NavigationIcon != null)
{
if (toolbar.NavigationIcon is DrawerArrowDrawable navigationIcon)
{
// Code goes here?
}
}
}
return result;
}
If this is indeed the correct path, what code goes in the area marked by the comment "Code goes here?"?
* UPDATE *
I realized that what I am trying to figure out was not exactly described in my original question.
More specifically, when I mentioned that I am trying to resize the navigation bar back arrow, what I am really trying to do is to resize the button that the icon appears on.
For example, if I shrink the height of the navigation bar using code like the following:
On<Android>().SetBarHeight(100);
The button that the icon appears on will be clipped.
Ultimately, what I am trying to accomplish is resizing the icon AND the button that the icon appears on. I have already figured out how to do the former.
I am attempting to change the size of the back arrow in the navigation bar.
If you want to change size of the back button in the navigation bar, you can get new icon from Drawable in resource for toolbar.NavigationIcon.
public class NavigationPageRenderer : Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer
{
public AppCompToolbar toolbar;
public Activity context;
protected override Task<bool> OnPushAsync(Page view, bool animated)
{
var retVal = base.OnPushAsync(view, animated);
context = (Activity)Forms.Context;
toolbar = context.FindViewById<AppCompToolbar>(Droid.Resource.Id.toolbar);
if (toolbar != null)
{
if (toolbar.NavigationIcon != null)
{
if (toolbar.NavigationIcon is DrawerArrowDrawable navigationIcon)
{
// Code goes here?
toolbar.NavigationIcon = Android.Support.V7.Content.Res.AppCompatResources.GetDrawable(context, Resource.Drawable.back);
toolbar.Title = "Back";
}
}
}
return retVal;
}
}
Here is the sample you can take a look:
https://github.com/hachi1030-Allen/XamarinCustomNavBar
<NavigationPage.TitleView> will help you out there. This is a XAML approach.
Example:
<NavigationPage.TitleView>
<StackLayout>
....
</StackLayout>
</NavigationPage.TitleView>
Using this approach you will be able to set HeightRequest and WidthRequest of whatever element you place inside the StackLayout and whatever else you want to amend.
Also, note that if you are having icon size problems it may be worth looking into whether or not your drawable/ icons are the right size for the right resolution.
Iam developing a xamarin forms application.In my application,the navigation Bar color is blue. I want navigation bar color of one specific page as white.
I achieved that through
((NavigationPage)Application.Current.MainPage).BarBackgroundColor = Color.White;
Now Iam facing a problem.The page which have white navigationbar will pop back to previous page after certain time.After the popasync, previous pages which have blue navigation bar will also turn to white color. How to avoid that?.
namespace sample
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ApprovedAnimation : ContentPage
{
public ApprovedAnimation ()
{
InitializeComponent ();
((NavigationPage)Application.Current.MainPage).BarBackgroundColor = Color.White;
Device.StartTimer(TimeSpan.FromMilliseconds(3000), () =>
{
Navigation.PopAsync();
return false;
});
}
}
}
You need to set back the navigation color on the page itself using this method
protected override void OnDisappearing()
{
base. OnDisappearing();
((NavigationPage)Application.Current.MainPage).BarBackgroundColor = Color.Blue; // this will set back the normal color
}
I have an issue, which hasn't been fixed by now. Since I have added a feature, so that the app starts by showing the loginviewController when the user is not logged in or going directly to the userViewcontroller which represents the app functions, everything which is created in the Storyboard is not visible and the app just shows a black screen.
I am working with Firebase. I have recently added the Facebook Button, which works perfectly fine. Since it was created without the Storyboard it is the only object to be shown in the app. After the user logs in with the Facebook button, it shows a black screen again.
I have added the following code to the app delegate:
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = MainViewController()
The MainController has the following code:
import UIKit
class MainViewController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .red
let isLoggedIn = false
if isLoggedIn {
}else {
perform(#selector(showLoginController), with: nil, afterDelay: 0.01)
}
}
func showLoginController() {
let loginController = LoginViewController()
present(loginController, animated: true, completion: {
})
}
}
Do you have any idea why it is not working properly?
I want to manage the nav bar appearence in the xaml?
And how can I manage the nav bar appearence in a Tabbed Page?
NavigationBar.SetHasNavigationBar(tabPage,true) seems not working.
Thank you all,
Ilenia
Maybe check that your mainpage is wrapped with NavigationPage in the public App() function:
MainPage = new NavigationPage(new LoginPage());
Changing the color of your navigation bar
To change the color of your navigation bar generically using Xamarin.Forms so that you only need to change the properties in one place, I'd suggest you add the following to your App.cs file like mentioned in this post on the Xamarin Forums.
Changing the color of the tab bar
Unfortunately, changing the color of the Tab Bar is currently not supported in xaml.
You will although need to write a customer renderer for iOS like so:
[assembly: ExportRenderer(typeof(TabbedPage), typeof(CustomTabbedPageRenderer))]
namespace JetAdvice_Free.iOS.Renderers
{
class CustomTabbedPageRenderer : TabbedRenderer
{
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
TabBar.TintColor = MonoTouch.UIKit.UIColor.Black;
TabBar.BarTintColor = MonoTouch.UIKit.UIColor.FromRGB(255, 128, 0);
}
}
}
Then, whenever you use a TappedPage on iOS, the background color will be applied to your tab bar.
You can set the navigationbar color only when you initialize your navigationpage like so:
new NavigationPage(your page here)
{
BarBackgroundColor = Color.Green,
BarTextColor = Color.White
};
For the issue of setting the tintcolor of the tabbar I recommend using a renderer like so:
public class TabPage_iOS : TabbedRenderer
{
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
this.TabBar.TintColor = Color.Blue.ToUIColor();
UITableView tableView = (UITableView)this.MoreNavigationController.TopViewController.View;
tableView.TintColor = this.TabBar.TintColor;
}
}
I'm trying to follow the standard approach to creating a custom UINavigationBar in order to change its background image, but have found an issue in the subclassing process. If I subclass UINavigationController, with the intent of overriding the virtual NavigationBar property to provide my own implementation, all navigation items (any left or right buttons, and the title view) disappear. At first I thought it was due to the background being rendered over top of the navigation items, but I can reproduce the problem with a no-op subclass.
It's reproducible with the following code:
[Register("NavigationBar")]
public class NavigationBar : UINavigationBar
{
public NavigationBar () : base()
{
}
public NavigationBar (NSCoder coder) : base(coder)
{
}
public NavigationBar (IntPtr ptr) : base(ptr)
{
}
public NavigationBar (NSObjectFlag t) : base(t)
{
}
public NavigationBar (RectangleF frame) : base(frame)
{
}
}
[Register("NavigationController")]
public class NavigationController : UINavigationController
{
private UINavigationBar _navBar;
public NavigationController () : base()
{
}
public NavigationController (NSCoder coder) : base(coder)
{
}
public NavigationController (IntPtr ptr) : base(ptr)
{
}
public NavigationController (NSObjectFlag t) : base(t)
{
}
public override UINavigationBar NavigationBar
{
get
{
if(_navBar == null)
{
return base.NavigationBar;
}
return _navBar;
}
}
public void SetNavigationBar(UINavigationBar navigationBar)
{
_navBar = (UINavigationBar)navigationBar;
}
}
Now, all you need to do to lose your navigation items is to use the custom classes instead of the default ones:
var navigationBar = new NavigationBar();
navigationBar.BarStyle = UIBarStyle.Black;
navigationBar.TintColor = HeaderColor;
var navigationController = new NavigationController();
navigationController.SetNavigationBar(navigationBar);
// ...
Well, your SetNavigationBar() method doesn't pass that down to the native base class and since you don't do any explicit drawing yourself, how is the native drawing code ever supposed to be invoked for your custom NavigationBar class?
In your example code, that NavigationBar is just floating around in space and never gets told to draw.
In order to subclass UINavigationBar, you must define the IntPtr constructor in your derived class and instantiate the UINavigationController using the public UINavigationController(Type navigationBarType, Type toolbarType) constructor. Example:
public class MyNavigationBar: UINavigationBar
{
public MyNavigationBar(IntPtr h) : base(h)
{
}
// Do something.
}
....
var navController = new UINavigationController(typeof(MyNavigationBar), typeof(UIToolbar));
Took me a while to figure it out. More information on this page: http://developer.xamarin.com/guides/ios/platform_features/introduction_to_ios_6/ in section Subclassing UINavigationBar.
Can you create a sample project where you're adding NavigationItems directly to a UINavigationController and then using the sub-classed UINavigationController/UINavigationBar causes these buttons to disappear?
Thanks,
ChrisNTR
After a lot of research and back and forth with Xamarin, the answer to this problem is that you must use an IB stub file that is essentially no-op, but exists to shuttle the desired base type for your navigation elements. There is a working example on my OSS project: http://github.com/danielcrenna/artapp