xcode 如何设置 Xamarin iOS 导航和状态栏颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24746690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set Xamarin iOS navigation and status bar color
提问by cheriana
I'm new to Xamarin iOS Designer and am trying to set the status bar and navigation controller background color as shown in this example: https://www.youtube.com/watch?v=DApI7aWGNiY
我是 Xamarin iOS Designer 的新手,正在尝试设置状态栏和导航控制器背景颜色,如下例所示:https: //www.youtube.com/watch?v=DApI7aWGNiY
I've opened my main storyboard in Xcode Interface Builder and followed the steps in the tutorial, adding code to appDelegate.m. Code is as follows:
我已经在 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
The above doesn't seem to work - the status and navigation bar remain gray when I save and build in Xamarin.
以上似乎不起作用 - 当我在 Xamarin 中保存和构建时,状态栏和导航栏保持灰色。
I'm wondering why and if it has anything to do with the fact that the project was created in Xamarin and not Xcode. My .m file resides in a "supporting files" folder unlike the demo and it doesn't contain any of the auto-generated code. There seems to be much better documentation on Xcode Interface Builder which is why I have been using that instead of the newer Xamarin iOS Designer.
我想知道为什么以及是否与项目是在 Xamarin 而不是 Xcode 中创建的事实有关。与演示不同,我的 .m 文件位于“支持文件”文件夹中,它不包含任何自动生成的代码。Xcode Interface Builder 上似乎有更好的文档,这就是为什么我一直使用它而不是较新的 Xamarin iOS Designer。
Xamarin Studio 5.1.3 and Xcode 5.1.1 Any tips? Thanks!
Xamarin Studio 5.1.3 和 Xcode 5.1.1 有什么提示吗?谢谢!
**Update: Was able to achieve the background color change on both status and nav bar by adding
this.NavigationController.NavigationBar.BarTintColor = UIColor.Yellow;
to my viewcontroller.cs right after base.ViewDidLoad ();
. Would still like to know why code added to the Xcode project .m files does not render in the Xamarin build.
**更新:通过添加 this.NavigationController.NavigationBar.BarTintColor = UIColor.Yellow,能够实现状态栏和导航栏的背景颜色变化;到我的 viewcontroller.cs 之后base.ViewDidLoad ();
。仍然想知道为什么添加到 Xcode 项目 .m 文件的代码不会在 Xamarin 构建中呈现。
回答by Nick Peppers
If you want to change the navigation bar color throughout your entire iOS app add this to your AppDelegate FinishedLaunching method:
如果您想更改整个 iOS 应用程序中的导航栏颜色,请将其添加到您的 AppDelegate FinishedLaunching 方法中:
UINavigationBar.Appearance.BarTintColor = UIColor.YourColor;
You can also edit the text attributes for the navigation bar by doing the following:
您还可以通过执行以下操作来编辑导航栏的文本属性:
UINavigationBar.Appearance.SetTitleTextAttributes(new UITextAttributes { TextColor = UIColor.White });
As for the status bar if you want to change the status bar icons to white instead of default black, for example, create a class for your navigation controller and override UIStatusBarStyle
至于状态栏,如果要将状态栏图标更改为白色而不是默认的黑色,例如,为您的导航控制器创建一个类并覆盖 UIStatusBarStyle
public override UIStatusBarStyle PreferredStatusBarStyle ()
{
return UIStatusBarStyle.LightContent;
}
This would work for your NavigationController and any child controllers of it.
这适用于您的 NavigationController 及其任何子控制器。
回答by Morgan Skinner
I finally got this working in Xamarin Forms so blogged about it, see http://morganskinner.blogspot.co.uk/2015/01/xamarin-forms-light-status-bar-for-ios.htmlfor full details. I also have a follow up post on setting a background image for the Navigation bar and have it working for Landscape too - see http://morganskinner.blogspot.co.uk/2015/01/xamarin-formsusing-background-images-on.html. Hope this helps someone else!
我终于在 Xamarin Forms 中完成了这个工作,所以写了一篇关于它的博客,请参阅http://morganskinner.blogspot.co.uk/2015/01/xamarin-forms-light-status-bar-for-ios.html了解完整详细信息。我还有一篇关于为导航栏设置背景图像的后续文章,并且它也适用于横向 - 请参阅http://morganskinner.blogspot.co.uk/2015/01/xamarin-formsusing-background-images-on html的。希望这对其他人有帮助!
回答by kiran
The below line code may be helpful for you.
以下行代码可能对您有所帮助。
public override void ViewDidAppear(bool animated){
NavigationController.NavigationBar.BackgroundColor = UIColor.Yellow;
}
回答by Bhavesh Nayi
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];
}