【问题标题】:How to manipulate properties of React Native - Native UI Component?如何操作 React Native - Native UI 组件的属性?
【发布时间】:2018-07-18 13:43:20
【问题描述】:

我正在开发一个 React Native 应用程序。

该应用由基于 Web 的视图和原生 ui 视图(组件)组成。

虽然基于 Web 的视图和原生 ui 视图的渲染工作正常,但我无法操作原生 ui 视图的属性,例如背景颜色。

我不得不说我的项目结构并不是那么简单,因为我做了很多桥接,从 React 到 Objective-C,从 Objective-C 到 Swift 等等。

简单介绍一下我的项目结构:

MyUIView.h

#import <UIKit/UIKit.h>

// Define which methods and properties have to be implemented in MyUIView
@interface MyUIView : UIView

@end

MyUIView.m

#import "MyUIView.h"

// Represents the native MyUIView
@implementation MyUIView
  -(instancetype) init {
    self = [super init];
    if (self) {
      [self setUp];
    }
    return self;
  }

  -(void) setUp {
    UIView * myUIView = [[UIView alloc] initWithFrame:CGRectMake(-50, -250, 100, 100)];
    [myUIView setBackgroundColor:[UIColor grayColor]];
    [self addSubview:myAudioRecorderUIView];
  }

@end

MyUIManager.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <React/RCTViewManager.h>

@interface MyUIManager : RCTViewManager
  @property (nonatomic, strong) UIView *myUIView;

  - (void) changeBackgroundColor: (UIColor*)color;
@end

MyUIManager.m

#import "MyUIManager.h"
#import "reactnative-Swift.h"

#import "MyUIView.h"
#import <UIKit/UIKit.h>

@import UIKit;

// Controls the rendering of native view in the React part of our application
@implementation MyUIManager
  MyUIManager *myUIManager;
  MyUIView *myUIView;

  + (void) initialize {
    myUIManager = [MyUIManager allocWithZone: nil];
    myUIView = [[MyUIView alloc] init];
  }

  + (id) allocWithZone:(NSZone *)zone {
    static MyUIManager *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
      sharedInstance = [super allocWithZone:zone];
    });
    return sharedInstance;
  }

  - (void) changeBackgroundColor: (UIColor*)color {
    dispatch_sync(dispatch_get_main_queue(), ^{
      self.myUIView.backgroundColor = UIColor.redColor;
    });
  }

  RCT_EXPORT_MODULE()

  - (UIView *) view
  {
    return myUIView;
  }

@end

MyViewController.swift

import Foundation
import UIKit

@objc open class MyViewController : UIViewController {

  let myUIManager: MyUIManager = MyUIManager();

  @objc func changeColor() {
    myUIManager.changeBackgroundColor(UIColor.red)
  }
}

我期望从我的 Swift 类调用 changeColor 方法会改变我的原生 ui 视图的颜色,因为 myUIManager 的 changeBackgroundColor 是在设置颜色的地方调用的。不过颜色确实变了。它保持灰色,就像在 MyUIView.m 的 setUp 方法中定义的一样。

有什么建议吗?

【问题讨论】:

    标签: ios objective-c swift react-native react-native-native-ui-component


    【解决方案1】:

    必须进行一些调整才能使其正常工作,最重要的是将应该操作的视图包装到另一个视图中,然后返回父视图而不是子视图:

    MyUIView.m

    #import "MyUIView.h"
    
    // Represents the native MyUIView
    @implementation MyUIView
      -(instancetype) init {
        self = [super init];
        if (self) {
          [self setUp];
        }
        return self;
      }
    
      - (instancetype)initWithFrame:(CGRect)frame {
        NSLog(@"init with frame: %@", NSStringFromCGRect(frame));
        self = [super initWithFrame:frame];
        if ( self ) {
          [self setUp];
        }
        return self;
      }
    
      - (void)setUp {
        self.backgroundColor = [UIColor grayColor];
      }
    
      - (void)layoutSubviews {
        NSLog(@"layout subviews");
      }
    
    @end
    

    MyUIManager.h

    #import <UIKit/UIKit.h>
    #import <React/RCTViewManager.h>
    
    @interface MyUIManager : RCTViewManager
      @property (nonatomic) UIView *myParentUIView;
      @property (nonatomic) UIView *myUIView;
    
      - (void) changeBackgroundColor: (UIColor*)color;
    @end
    

    MyUIManager.m

    #import "MyUIManager.h"
    #import "reactnative-Swift.h"
    
    #import "MyUIManager.h"
    #import "MyUIView.h"
    #import <UIKit/UIKit.h>
    
    // Controls the rendering of native view in the React part of our application
    @implementation MyUIManager
      MyUIView *myParentUIView;
      MyUIView *myUIView;
    
      // Instantiate MyUIView (parent and child)
      + (void) initialize {
        myParentUIView = [[MyUIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        myUIView = [[MyUIView alloc] initWithFrame:CGRectMake(-50, -250, 100, 100)];
        [myParentUIView addSubview:myUIView];
      }
    
      // Change background color of view
      - (void) changeBackgroundColor: (UIColor*)color {
        dispatch_sync(dispatch_get_main_queue(), ^{
          myParentUIView.subviews.firstObject.backgroundColor = color;
        });
      }
    
      RCT_EXPORT_MODULE()
    
      - (UIView *)view {
        return myParentUIView;
      }
    
    @end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-24
      • 1970-01-01
      • 2021-01-08
      • 1970-01-01
      • 2016-03-31
      • 1970-01-01
      • 2020-11-23
      • 2016-09-05
      相关资源
      最近更新 更多