【问题标题】:React Native iOS NavigatorIOS title fontReact Native iOS NavigatorIOS 标题字体
【发布时间】:2016-01-23 14:03:59
【问题描述】:

我目前正在开发一个 React Native 并且我想更改 NavigatorIOS 标题的字体,我从Github issueStack Overflow 找到了一些有希望的链接。然而遗憾的是,两者都没有帮助。

我目前在我的 AppDelegate.m 中有这段代码

[[UINavigationBar appearance] setTitleTextAttributes:@{
 NSFontAttributeName : [UIFont fontWithName:@"Avenir-Light" size:22.0],
 NSForegroundColorAttributeName : [UIColor whiteColor]
}];

这也不会改变标题栏的字体,而不是 NavigatorIOS 在开始时给出的字体。

【问题讨论】:

  • 你能解决这个问题吗?

标签: ios react-native


【解决方案1】:

你把这段代码放在AppDelegate哪里。

最好放入一个RCT_EXPORT_METHORD,它应该可以从任何 js 类中调用。

在 AppDelagete.h 中

import BridgeModule.h
Add RCTBridgeModule protocol

在 AppDelegate.m 中

Add:  RCT_EXPORT_MODULE() below implementation
@implementation AppDelegate
RCT_EXPORT_MODULE()

添加这个新功能

RCT_EXPORT_METHOD(updateNavigationBar){

    [[UINavigationBar appearance] setTitleTextAttributes:@{
    NSFontAttributeName : [UIFont fontWithName:@"Avenir-Light" size:22.0],
    NSForegroundColorAttributeName : [UIColor whiteColor]

}];

}

js 的变化

添加以下行

var AppDelegate = require('NativeModules').AppDelegate;

someJSFunction{

    AppDelegate.updateNavigationBar();

}

这将有效。 (此处输入的代码)

【讨论】:

    【解决方案2】:

    我不相信 Rahul 发布的答案会起作用,因为 React Native 编码导航栏外观的方式发生了变化。

    我制作了一个小补丁,可以让您的代码正常工作。我可能会将此提交给 React Native,但尚未决定:

    --- a/node_modules/react-native/React/Views/RCTWrapperViewController.m
    +++ b/node_modules/react-native/React/Views/RCTWrapperViewController.m
    @@ -115,9 +115,11 @@ static UIView *RCTFindNavBarShadowViewInView(UIView *view)
         bar.barTintColor = _navItem.barTintColor;
         bar.tintColor = _navItem.tintColor;
         bar.translucent = _navItem.translucent;
    -    bar.titleTextAttributes = _navItem.titleTextColor ? @{
    -      NSForegroundColorAttributeName: _navItem.titleTextColor
    -    } : nil;
    +    if (_navItem.titleTextColor != nil) {
    +        NSMutableDictionary *newAttributes = bar.titleTextAttributes ? bar.titleTextAttributes.mutableCopy :  [NSMutableDictionary new];
    +        [newAttributes setObject:_navItem.titleTextColor forKey:NSForegroundColorAttributeName];
    +        bar.titleTextAttributes = newAttributes;
    +    }
    
         RCTFindNavBarShadowViewInView(bar).hidden = _navItem.shadowHidden;
    

    使用此补丁,您应该能够执行以下操作 (Swift):

     UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont(name:"Avenir-Light", size: 22.0) as Any]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-11
      • 2015-08-24
      • 2017-09-04
      相关资源
      最近更新 更多