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

如何设置Xamarin iOS导航和状态栏颜色,ios,xcode,xamarin.ios,xamarin,Ios,Xcode,Xamarin.ios,Xamarin,我是Xamarin iOS Designer的新手,正在尝试设置状态栏和导航控制器背景颜色,如本例所示: 我已经在Xcode Interface Builder中打开了我的主情节提要,并按照教程中的步骤向appDelegate.m添加了代码。代码如下: #import "AppDelegate.h" #define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >>

我是Xamarin iOS Designer的新手,正在尝试设置状态栏和导航控制器背景颜色,如本例所示:

我已经在Xcode Interface Builder中打开了我的主情节提要,并按照教程中的步骤向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中创建的这一事实有关。与演示不同,My.m文件位于“supporting files”文件夹中,不包含任何自动生成的代码。Xcode Interface Builder上似乎有更好的文档,这就是为什么我一直使用它而不是更新的Xamarin iOS设计器

Xamarin Studio 5.1.3和Xcode 5.1.1 有什么建议吗?谢谢

**更新:通过添加 this.NavigationController.NavigationBar.BarTintColor=UIColor.Yellow;
base.ViewDidLoad()之后立即转到我的viewcontroller.cs。仍然想知道为什么添加到Xcode project.m文件的代码不会在Xamarin构建中呈现

下面的代码可能会对您有所帮助

public override void ViewDidAppear(bool animated){

    NavigationController.NavigationBar.BackgroundColor = UIColor.Yellow;

} 
if([[[UIDevice currentDevice]systemVersion]floatValue]<7.0)
{
[[UINavigationBar外观]setBackgroundImage:[UIImage ImageName:@”“]forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar外观]setTitleTextAttributes:
@{
UITextAttributeTextColor:[UIColor whiteColor],UITextAttributeTextShadowColor:[UIColor clearColor],UITextAttributeTextShadowOffset:[NSValue valueWithUIOffset:UIOffsetMake(0.0f,1.0f)],UITextAttributeFont:[UIFont fontWithName:@“ArialMT”大小:18.0f]
}];
CGFloat垂直偏移=0;
[[UINavigationBar外观]setTitleVerticalPositionAdjustment:BarMetrics的垂直偏移量:UIBarMetricsDefault];
}
其他的
{
[[UINavigationBar外观]SetPartIntColor:[UIColor whiteColor]];
//取消注释以更改“后退”按钮的颜色
[[UINavigationBar外观]setTintColor:[UIColor whiteColor]];
//取消注释以指定自定义背景图像
[[UINavigationBar外观]setBackgroundImage:[UIImage ImageName:@”“]forBarMetrics:UIBarMetricsDefault];
//取消注释以更改后指示器图像
[[UINavigationBar外观]设置指示符号图像:[UIImage ImageName:@”“];
[[UINavigationBar外观]收件指示符号转换图像:[UIImage ImageName:@”“];
//取消注释以更改标题的字体样式
NSShadow*shadow=[[NSShadow alloc]init];
shadow.shadowColor=[UIColor colorWithRed:0.0绿色:0.0蓝色:0.0 alpha:0.8];
shadow.shadowOffset=CGSizeMake(0,1);
[[UINavigationBar外观]setTitleTextAttributes:[NSDictionary Dictionary With Objects and Keys:[UIColor ColorWith红色:245.0/255.0绿色:245.0/255.0蓝色:245.0/255.0 alpha:1.0],NSForegroundColorAttributeName,shadow,NSShadowAttributeName,[UIFontWithName:@“ArialMT”大小:18.0],NSFontAttributeName,nil];
CGFloat垂直偏移=0;
[[UINavigationBar外观]setTitleVerticalPositionAdjustment:BarMetrics的垂直偏移量:UIBarMetricsDefault];
}

如果要在整个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及其任何子控制器

我终于在Xamarin表单中实现了这一点,因此我在博客中对此进行了讨论,请参阅以了解详细信息。我还有一篇关于为导航栏设置背景图像的后续文章,并让它也适用于景观-请参阅。希望这对其他人有帮助

再想想,如果您确实以这种方式更改了它,您可能希望在(type)中包含UINavigationBar.appearance;我需要的是,不要更改某些iOS 7的弹出窗口和内容的默认样式,而是将UIViewControllerBasedStatusBarAppearance添加到plist源代码并将其设置为“否”。
public override UIStatusBarStyle PreferredStatusBarStyle ()
{
     return UIStatusBarStyle.LightContent;
}