Ios 在导航栏中将动态大小的“标题视图”居中,而不使用自动布局

Ios 在导航栏中将动态大小的“标题视图”居中,而不使用自动布局,ios,objective-c,react-native,titleview,Ios,Objective C,React Native,Titleview,我不熟悉iOS布局,我正试图以编程方式将自定义视图(实际上是一个React NativeRCTrotView,它扩展了UIView,如果相关的话),它具有固有大小的动态内容(在javascript中定义) 我见过解决方案覆盖intrinsicallySizedContent,但这似乎只与自动布局相关,而RN不使用自动布局。我能够测量rcrootview内容大小的唯一方法是将其添加到视图中,然后等待几次layoutSubViews,直到其frame具有大小。这使我做出了以下努力: 尝试:在Layo

我不熟悉iOS布局,我正试图以编程方式将自定义视图(实际上是一个React Native
RCTrotView
,它扩展了
UIView
,如果相关的话),它具有固有大小的动态内容(在javascript中定义)

我见过解决方案覆盖
intrinsicallySizedContent
,但这似乎只与自动布局相关,而RN不使用自动布局。我能够测量
rcrootview
内容大小的唯一方法是将其添加到视图中,然后等待几次
layoutSubViews
,直到其
frame
具有大小。这使我做出了以下努力:

尝试:在LayoutSubView中包装并设置包装大小 我正在另一个
UIView
中包装
rcrootview
,并试图覆盖
layoutSubViews
,一旦我有了本机内容的大小,就设置包装框的大小

我的包装被创建并添加到My
UIViewController
中的导航栏中,如下所示:

  RCTBridge *bridge = ((RCTRootView*)self.view).bridge;
  RCTRootView *reactView = [[RCTRootView alloc] initWithBridge:bridge moduleName:navBarCustomView initialProperties:initialProps];
  RCCCustomTitleView *titleView = [[TitleWrapperView alloc] initWithFrame:self.navigationController.navigationBar.bounds subView:reactView];
  self.navigationItem.titleView = titleView;
  self.navigationItem.titleView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
#import "TitleWrapperView.h"
#import <React/RCTRootView.h>

@interface TitleWrapperView ()
@property (nonatomic, strong) RCTRootView *subView;
@end

@implementation TitleWrapperView // Extends UIView

-(instancetype)initWithFrame:(CGRect)frame subView:(RCTRootView*)subView {
    self = [super initWithFrame:frame];
    
    if (self) {
        self.subView = subView;
        self.subView.sizeFlexibility = RCTRootViewSizeFlexibilityWidthAndHeight;
        self.clipsToBounds = true;

        // Prevent the wrapper from being so large initially as to squash the '< Back' button down to '<'
        self.frame = CGRectZero;

        [self addSubview:subView];
    }
    
    return self;
}

-(void)layoutSubviews {
    [super layoutSubviews];
    CGRect contentViewFrame = self.subView.contentView.frame;
    if (contentViewFrame.size.width > 0) {
        // Once we have a measurement for the sub-view's content, set the wrapper's frame
        if (self.frame.size.width != contentViewFrame.size.width) {
            self.frame = contentViewFrame;
        }
    }
}
@end
包装器的实现方式如下:

  RCTBridge *bridge = ((RCTRootView*)self.view).bridge;
  RCTRootView *reactView = [[RCTRootView alloc] initWithBridge:bridge moduleName:navBarCustomView initialProperties:initialProps];
  RCCCustomTitleView *titleView = [[TitleWrapperView alloc] initWithFrame:self.navigationController.navigationBar.bounds subView:reactView];
  self.navigationItem.titleView = titleView;
  self.navigationItem.titleView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
#import "TitleWrapperView.h"
#import <React/RCTRootView.h>

@interface TitleWrapperView ()
@property (nonatomic, strong) RCTRootView *subView;
@end

@implementation TitleWrapperView // Extends UIView

-(instancetype)initWithFrame:(CGRect)frame subView:(RCTRootView*)subView {
    self = [super initWithFrame:frame];
    
    if (self) {
        self.subView = subView;
        self.subView.sizeFlexibility = RCTRootViewSizeFlexibilityWidthAndHeight;
        self.clipsToBounds = true;

        // Prevent the wrapper from being so large initially as to squash the '< Back' button down to '<'
        self.frame = CGRectZero;

        [self addSubview:subView];
    }
    
    return self;
}

-(void)layoutSubviews {
    [super layoutSubviews];
    CGRect contentViewFrame = self.subView.contentView.frame;
    if (contentViewFrame.size.width > 0) {
        // Once we have a measurement for the sub-view's content, set the wrapper's frame
        if (self.frame.size.width != contentViewFrame.size.width) {
            self.frame = contentViewFrame;
        }
    }
}
@end
#导入“titleRapperView.h”
#进口
@接口标题RapperView()
@属性(非原子,强)RCTRootView*子视图;
@结束
@实现标题rapperview//扩展UIView
-(instancetype)initWithFrame:(cRect)框架子视图:(RCTRootView*)子视图{
self=[super initWithFrame:frame];
如果(自我){
self.subView=子视图;
self.subView.sizeFlexibility=rctrootviewsizeflexibilitywidthandhight;
self.clipstobunds=true;
//防止包装器最初太大,以至于将“