【问题标题】:How to design a view using componentKit in iOS如何在 iOS 中使用 componentKit 设计视图
【发布时间】:2026-01-09 22:05:01
【问题描述】:

我想在objective-c 中使用componentKit 创建一个自定义视图。该视图看起来像 facebook 应用程序中的新闻源视图,我编写了以下代码来制作该视图,

+ (instancetype)newWithText:(NSString *)text
                context:(QuoteContext *)context
          {

return [super newWithComponent:
        [QuoteWithBackgroundComponent
         newWithBackgroundImage:[context imageNamed:@""]
         quoteComponent:

         [CKInsetComponent
          newWithInsets:{.top = 10, .bottom = 10, .left = 10, .right = 10}
          component:
          [CKFlexboxComponent
           newWithView:{}
           size:{}
           style:{
               .direction = CKFlexboxDirectionVertical,
               //                 .spacing = 10,
           }
           children:{
               {
                   // Profile Picture
                   [CKImageComponent
                    newWithImage: nil
                    attributes:{
                        {@selector(setBackgroundColor:), [UIColor darkGrayColor]},
                        {CKComponentViewAttribute::LayerAttribute(@selector(setCornerRadius:)), 17.5}
                    }
                    size:CKComponentSize::fromCGSize(CGSizeMake(35, 35))],
               },
               {
                   // Name
                   [CKInsetComponent
                    newWithInsets:{ .right= 140, .top= -35}
                    component:
                    [CKLabelComponent
                     newWithLabelAttributes:{
                         .string = @"Asim Iftikhar Abbasi",
                         .font = [UIFont fontWithName:@"Baskerville" size:20],
                     }

                     viewAttributes:{
                         {@selector(setBackgroundColor:), [UIColor whiteColor]},
                         {@selector(setUserInteractionEnabled:), @NO},
                     }
                     size:{ }]],
                   .alignSelf = CKFlexboxAlignSelfEnd, // Right aligned
               },
               {
                   // Location
                   [CKInsetComponent
                    newWithInsets:{.right= 185, .top= -10}
                    component:
                    [CKLabelComponent
                     newWithLabelAttributes:{
                         .string = @"2 hrs Islamabad, Pakistan",
                         .font = [UIFont fontWithName:@"Baskerville" size:12]
                     }
                     viewAttributes:{
                         {@selector(setBackgroundColor:), [UIColor whiteColor]},
                         {@selector(setUserInteractionEnabled:), @NO},
                     }
                     size:{ }
                     ]],
                   .alignSelf = CKFlexboxAlignSelfEnd, // Right aligned
               },
               {
                   // Post
                   [CKInsetComponent
                    newWithInsets:{.right= 0, .top= 0}
                    component:
                    [CKImageComponent
                     newWithImage: nil
                     attributes:{{@selector(setBackgroundColor:), [UIColor blackColor]},
                     }
                     size:CKComponentSize::fromCGSize(CGSizeMake(400, 200))]],
                   .spacingBefore = 10
               },
               {
                   // Buttons
                   [CKInsetComponent
                    newWithInsets:{.right= 0, .top= 15}
                    component:
                    [CKButtonComponent
                     newWithAction:{}
                     options:{
                         .titles = @"Hello",
                         .attributes = {{@selector(setHighlighted:), @YES}},
                         .titleColors = titleColors,
                         .selected = YES,
                     }]],
                   .spacingBefore = 10
               },
           }]]]];
      }
     @end

我无法访问选项的属性

 options:{
                     .titles = @"Hello",
                     .attributes = {{@selector(setHighlighted:), @YES}},
                     .titleColors = titleColors,
                     .selected = YES,
                 }

当我访问buttontitles 属性时,编译器会生成以下错误: use of undeclared identifier titles

请任何人帮助我...在此先感谢

【问题讨论】:

  • 能否添加产生错误的代码部分?
  • [CKInsetComponent newWithInsets:{.right= 0, .top= 15} 组件:[CKButtonComponent newWithAction:{} 选项:{ .titles = @"Hello", .attributes = {{@selector( setHighlighted:), @YES}}, .titleColors = titleColors, .selected = YES, }]] 在这我无法获得选项属性
  • 好吧,我猜是类型不对。试试.titles = {{UIControlStateNormal, @"Hello"}}

标签: ios objective-c objective-c++ componentkit


【解决方案1】:

这里的 ComponentKit expects from you 不是单个字符串,而是UIControlStates 和字符串之间的映射,以便您可以为UIControlStateNormalHighlightedDisabled 等定义不同的字符串。您可以指定为:

.titles = {{UIControlStateNormal, @"Hello"}}

但是,如果您不关心映射默认为正常状态的has another constructor 的其他状态,则应按如下方式工作:

.titles = {@"Hello"}

希望有帮助!

【讨论】:

  • 当我尝试这个编译器产生错误No matching constructor for initialization of 'const CKButtonComponentOptions &