更改导航栏标题的颜色

时间:2014-08-06 07:17:01

标签: c# ios xamarin.ios xamarin uinavigationbar

我尝试使用UINavigationBar设置Xamarin MonoTouch的样式。在UIViewController的构造函数中,我尝试了以下内容:

//this.NavigationController.NavigationBar.TintColor = UIColor.Magenta;
UINavigationBar.Appearance.TintColor = UIColor.Yellow;

但是,如果我尝试在模拟器中运行它,则没有任何改变。我应该在哪里放置此代码?如何使用RGB颜色(UIColor.FromRGB (0, 127, 14)?)我的代码是否正确?

4 个答案:

答案 0 :(得分:3)

我的解决方案:

//AppDelegate.cs
public partial class AppDelegate : UIApplicationDelegate
{
    // class-level declarations
    UIWindow window;
    public static UIStoryboard Storyboard = UIStoryboard.FromName ("MainStoryboard", null);
    public static UIViewController initialViewController;

    // ...
    public override bool FinishedLaunching (UIApplication app, NSDictionary options)
    {
        window = new UIWindow (UIScreen.MainScreen.Bounds);
        initialViewController = Storyboard.InstantiateInitialViewController () as UIViewController;

        UINavigationBar.Appearance.SetTitleTextAttributes (
            new UITextAttributes () { TextColor = UIColor.FromRGB (0, 127, 14) });

        window.RootViewController = initialViewController;
        window.MakeKeyAndVisible ();
        return true;
    }
}

答案 1 :(得分:2)

假设你正在为iOS7开发,你需要设置BarTintColor。

UINavigationBar.Appearance.BarTintColor = UIColor.Red;
UINavigationBar.Appearance.BarTintColor = UIColor.FromRGB(0, 127, 14);

请参阅:How to change UINavigationBar background color from the AppDelegate

将此代码放在AppDelegate.cs

答案 2 :(得分:0)

试试这个..

NSDictionary *textTitleOptions = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor darkGrayColor], UITextAttributeTextColor, [UIColor whiteColor], UITextAttributeTextShadowColor, nil];
[[UINavigationBar appearance] setTitleTextAttributes:textTitleOptions];

或者你可以参考更多 IOS 7 Navigation Bar text and arrow color

答案 3 :(得分:0)

你应该具体使用UIViewController,如:

viewController.navigationBar.tintColor = [UIColor yellowColor];

或在UIViewController中

self.navigationBar.tintColor = [UIColor yellowColor];