如何设置Xamarin iOS导航栏和状态栏颜色

3

我是Xamarin iOS Designer的新手,正在尝试设置状态栏和导航控制器的背景颜色,就像这个示例中所示:https://www.youtube.com/watch?v=DApI7aWGNiY

我已经在Xcode Interface Builder中打开了我的主Storyboard,并按照教程中的步骤添加了代码到appDelegate.m文件中,代码如下:

#import "AppDelegate.h"
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >>    16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary   *)launchOptions
{

[[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x067AB5)];

return YES;
}

@end

上述方法似乎不起作用 - 当我在Xamarin中保存和构建时,状态栏和导航栏仍然是灰色的。
我想知道为什么,是否与该项目是在Xamarin而不是Xcode中创建有关。我的.m文件位于“supporting files”文件夹中,不像演示那样包含任何自动生成的代码。 Xcode Interface Builder似乎有更好的文档,这就是为什么我一直在使用它而不是较新的Xamarin iOS Designer的原因。
Xamarin Studio 5.1.3和Xcode 5.1.1 有什么提示吗?谢谢!
**更新:通过在base.ViewDidLoad()之后将以下内容添加到我的viewcontroller.cs中,我能够实现状态栏和导航栏的背景颜色更改。 this.NavigationController.NavigationBar.BarTintColor = UIColor.Yellow; 仍然想知道为什么添加到Xcode项目.m文件中的代码不会在Xamarin构建中呈现。
4个回答

15

如果您想在整个iOS应用程序中更改导航栏的颜色,请将以下代码添加到AppDelegate FinishedLaunching方法中:

UINavigationBar.Appearance.BarTintColor = UIColor.YourColor;
您还可以通过以下方式编辑导航栏的文本属性:
UINavigationBar.Appearance.SetTitleTextAttributes(new UITextAttributes { TextColor = UIColor.White });

如果您想将状态栏图标从默认的黑色更改为白色,例如,可以为导航控制器创建一个类并覆盖UIStatusBarStyle。

public override UIStatusBarStyle PreferredStatusBarStyle ()
{
     return UIStatusBarStyle.LightContent;
}

这将适用于您的 NavigationController 以及其中任何子控制器。


转念一想,如果您确实要以那种方式进行更改,您可能需要执行UINavigationBar.AppearanceWhenContainedIn(type)操作,这样就不会更改iOS 7的默认弹出窗口和其他元素的样式。 - Nick Peppers

7

1
感谢将UIViewControllerBasedStatusBarAppearance添加到plist源并将其设置为No,这正是我所需要的。 - shane

0
以下代码行可能对您有所帮助。
public override void ViewDidAppear(bool animated){

    NavigationController.NavigationBar.BackgroundColor = UIColor.Yellow;

} 

-2
    if([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0)
    {
        [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
        [[UINavigationBar appearance] setTitleTextAttributes:
         @{
           UITextAttributeTextColor: [UIColor whiteColor],UITextAttributeTextShadowColor: [UIColor clearColor],UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)],UITextAttributeFont: [UIFont fontWithName:@"ArialMT" size:18.0f]
           }];
        CGFloat verticalOffset = 0;
        [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:verticalOffset forBarMetrics:UIBarMetricsDefault];
    }
    else
    {
        [[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];

        // Uncomment to change the color of back button
        [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];

        // Uncomment to assign a custom backgroung image
        [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];

        // Uncomment to change the back indicator image

        [[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@""]];
        [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@""]];

        // Uncomment to change the font style of the title

        NSShadow *shadow = [[NSShadow alloc] init];
        shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
        shadow.shadowOffset = CGSizeMake(0, 1);

        [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,shadow, NSShadowAttributeName,[UIFont fontWithName:@"ArialMT" size:18.0], NSFontAttributeName, nil]];

        CGFloat verticalOffset = 0;
        [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:verticalOffset forBarMetrics:UIBarMetricsDefault];
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接