2017-08-01 73 views
0

我在iOS上创建本机UI组件,我希望它的大小根据内容大小进行扩展。 看来我必须设置一个固定的宽度和高度才能渲染视图。任何想法如何解决它?React原生本机UI组件宽度和高度

// JS

import React from 'react'; 
import { View, requireNativeComponent } from 'react-native'; 

class StyledText extends React.Component { 
render() { 
    return (
     <View style={this.props.style}> 
// without the height and width the compnent won't show up 
      <StyledLabelReactBridge styledText={'some text'} style={{height: 100, width: 100, backgroundColor: 'red'}}/> 
     </View> 
    ); 
} 
} 

StyledText.propTypes = { 
styledText: React.PropTypes.string, 
style: View.propTypes.style 
}; 

const StyledLabelReactBridge = requireNativeComponent('StyledLabelReactBridge', StyledText); 

module.exports = StyledText; 

//目标-C

@implementation StyledLabelReactBridgeManager 

RCT_EXPORT_MODULE() 

- (UIView *)view 
{ 
return [[NewStyledLabel alloc] init]; 
} 

RCT_CUSTOM_VIEW_PROPERTY(styledText, NSString, NewStyledLabel) 
{ 
if (![json isKindOfClass:[NSString class]]) 
    return; 

[view setStyledText:[NewStyledText textFromXHTML:json]]; 
} 

@end 

回答

1

你需要覆盖在Xcode reactSetFrame接收内容的大小变化。

#import "React/UIView+React.h" 

@implementation YourView { 
    - (void)reactSetFrame:(CGRect)frame { 
     [super reactSetFrame: frame]; 
     /* everytime content size changes, you will get its frame here. */ 
    } 
} 
+0

这救了我一命,应该是正确答案 – Porizm

-1

好了,找不到“自动”样的行为,该组件怎么过的,设置为:

{{ width: '100%', height: '100%}} 

使得它根据父,这是不够好扩张(和收缩)为我的用例。设置'flex:1'并不具有相同的效果令人遗憾。