Skip to content

Tutorial 4: Use custom view in xml layout

zhenglibao edited this page Jan 13, 2018 · 1 revision

The custom view has two requirements:

  • All the initiation must be done in init method. All other initWith... method will not be called.
  • Use FLEXSET macro to extend the view attribute. All the extended attribute will be available in xml layout Then you can use it in xml layout like any system controls.

If the initiation can't be done in init method, you can override createView:Name: method to make custom initiation.

-(UIView*)createView:(Class)cls
                Name:(NSString*)name
{
    if([@"_segment" compare:name]==0){     // custom initiation
        NSArray* items=@[@"item1",@"item2",@"item3"];
        return [[UISegmentedControl alloc]initWithItems:items];
    }
    return nil;  
}
Clone this wiki locally