自定义TabBar

-(instancetype)initWithArray:(NSArray*)array{

self= [superinit];

if(self) {

CGRectoldTabBarFrame =self.tabBar.frame;

UIView*myView = [[UIViewalloc]initWithFrame:oldTabBarFrame];

[self.viewaddSubview:myView];

CGFloatwidth = [UIScreenmainScreen].bounds.size.width/array.count;

for(NSIntegeri =0; i

UIButton* button = [UIButtonbuttonWithType:(UIButtonTypeSystem)];

[buttonsetTitle:[arrayobjectAtIndex:i]forState:(UIControlStateNormal)];

button.frame=CGRectMake(width * i,0, width, oldTabBarFrame.size.height);

button.tag=1000+ i;

button.backgroundColor= [UIColorblackColor];

[buttonsetTitleColor:[UIColorwhiteColor]forState:(UIControlStateNormal)];

[myViewaddSubview:button];

[buttonaddTarget:selfaction:@selector(didClickButton:)forControlEvents:(UIControlEventTouchUpInside)];

}

}

returnself;

}

-(void)didClickButton:(UIButton*)button{

self.selectedIndex= button.tag-1000;

}

你可能感兴趣的:(自定义TabBar)