我刚刚开始玩ComponentKit,我在水平排列两个标签时遇到了一些困难。我要第一个靠近左边边缘,第二个靠近右边。
有了自动布局,我可以使用这组约束来完成它:
H:|-0-[_label1]-[_label2]-0-|
我尝试的每一件事似乎都不起作用,我总是得到相同的结果:两个标签左对齐。
这是超级简单的组件:
+ (instancetype)newWithText1:(NSString *)text1 text2:(NSString *)text2
{
return [super
newWithComponent:
[CKStackLayoutComponent
newWithView:{}
size:{}
style:{
.direction = CKStackLayoutDirectionHorizontal,
.alignItems = CKStackLayoutAlignItemsCenter
}
children:{
{
[CKLabelComponent
newWithLabelAttributes:{
.string = text1,
.font = [UIFont systemFontOfSize:20],
.color = [UIColor blackColor]
}
viewAttributes:{
{@selector(setBackgroundColor:), [UIColor clearColor]}
}],
.alignSelf = CKStackLayoutAlignSelfStretch
},
{
[CKLabelComponent
newWithLabelAttributes:{
.string = text2,
.font = [UIFont systemFontOfSize:20],
.color = [UIColor redColor],
.alignment = NSTextAlignmentRight
}
viewAttributes:{
{@selector(setBackgroundColor:), [UIColor clearColor]}
}],
.alignSelf = CKStackLayoutAlignSelfStretch
}
}]];
}如果有人有什么建议会很感激的话。
发布于 2015-04-05 18:00:33
伟大的发现-这是我们的柔性盒实现(CKStackLayoutComponent)的一个缺点。您正在寻找类似于justify-content: space-between;的内容,但我们还不支持它。
如果您想向堆栈布局实现提交一个补丁以支持这一点,我们将不胜感激。否则,我会设法找一个人来增加支持。
现在,您可以通过使用flexGrow =YES添加一个间隔来伪造它:
+ (instancetype)newWithText1:(NSString *)text1 text2:(NSString *)text2
{
return [super
newWithComponent:
[CKStackLayoutComponent
newWithView:{}
size:{}
style:{
.direction = CKStackLayoutDirectionHorizontal,
.alignItems = CKStackLayoutAlignItemsCenter
}
children:{
{
[CKLabelComponent
newWithLabelAttributes:{
.string = text1,
.font = [UIFont systemFontOfSize:20],
.color = [UIColor blackColor]
}
viewAttributes:{
{@selector(setBackgroundColor:), [UIColor clearColor]}
}],
.alignSelf = CKStackLayoutAlignSelfStretch
},
{
[CKComponent new],
.flexGrow = YES,
},
{
[CKLabelComponent
newWithLabelAttributes:{
.string = text2,
.font = [UIFont systemFontOfSize:20],
.color = [UIColor redColor],
.alignment = NSTextAlignmentRight
}
viewAttributes:{
{@selector(setBackgroundColor:), [UIColor clearColor]}
}],
.alignSelf = CKStackLayoutAlignSelfStretch
}
}]];
}https://stackoverflow.com/questions/29460148
复制相似问题