OGeek|极客世界-中国程序员成长平台

标题: ios - Xamarin 表单工具栏/导航栏格式 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 14:32
标题: ios - Xamarin 表单工具栏/导航栏格式

我目前正在学习 Xamarin Forms,我首先重新创建了一个我以前为 iOS 开发的应用程序。我一直在尝试格式化导航栏(我认为它在表单中称为工具栏),甚至不知道我想要做什么。

This took me >5 minutes to knock together in xcode

This is currently what my xamarin project looks like

首先,出于某种原因,我的条形按钮被正确分组,我从 2014 年开始看到一些关于这不可能的旧帖子。他们改变了吗?我知道 Xamarin 自 2014 年以来发生了很大变化,我找不到最近被问到的问题(也许不可能??)。

其次,我在 iOS 中的页面颜色在导航栏下可见。它不在 Xamarin 中,我使用以下代码设置背景颜色:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    x:Class="NavigationDemo.MenuPage"
    BackgroundColor="Fuchsia"
    Title = "Navigation Bar">

这肯定应该延伸到后面吗?

所以对于一个 Xamarin 菜鸟,我该如何设置它,以便 iOS 栏按钮出现在左/右而不是右/右......以及如何让我的内容页面的背景颜色出现在导航下方/工具栏?

谢谢



Best Answer-推荐答案


启用透明度:

var navPage = new NavigationPage(new MyContentPage());

switch (Device.RuntimePlatform)
{
    case Device.iOS:
        navPage.On<Xamarin.Forms.PlatformConfiguration.iOS>()
            .EnableTranslucentNavigationBar();
        break;
}

我没有在不是导航页面的页面上测试它,但我想它应该可以工作。您可以尝试 var myPage = new MyContentPage(); 代替。

更改导航栏中文本/图标的颜色:

AppDelegate.cs 中,创建一个名为 InitNavbarAndTabBarAppearance() 的方法,并在初始化 Xamarin 之前在 FinishedLaunching() 方法中调用它.Forms:

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
    ...
    InitNavBarAndTabBarAppearance();

    global::Xamarin.Forms.Forms.Init();
    ...
}

InitNavbarAndTabBarAppearance():

private void InitNavBarAndTabBarAppearance()
{
    // Color of the navigation bar title:
    UINavigationBar.Appearance.SetTitleTextAttributes(
        new UITextAttributes()
        {
            TextColor = UIColor.Black,
        });

    // Color of the navigation bar buttons/toolbar items:
    UINavigationBar.Appearance.TintColor = UIColor.FromRGB(0, 122, 255);

    // Color of the selected tab icon & text:
    UITabBarItem.Appearance.SetTitleTextAttributes(
        new UITextAttributes()
        {
            TextColor = UIColor.FromRGB(0, 122, 255)
        }, 
        UIControlState.Selected);

    // Color of the unselected tab icon & text:
    UITabBarItem.Appearance.SetTitleTextAttributes(
        new UITextAttributes()
        {
            TextColor = UIColor.FromRGB(146, 146, 146)
        }, 
        UIControlState.Normal);
}

左侧工具栏项:

要在导航栏的左侧显示图标,您需要一个自定义渲染器。见这里:https://timeyoutake.it/2016/01/02/creating-a-left-toolbaritem-in-xamarin-forms/

关于ios - Xamarin 表单工具栏/导航栏格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48648985/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4