mardi 27 janvier 2015

UIToolbar left and right paddings are initially negative

I'm struggling with UIToolbar's strange behaviour in iOS7+ iPhone application.


Here's how I created it:



//toolbar
self.bottomToolbar=[[UIToolbar alloc] initWithFrame:CGRectMake(0.0, self.view.bounds.size.height-44.0, self.view.bounds.size.width, 44.0)];
self.bottomToolbar.autoresizingMask=(UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth);
[MKUIHelper toolbar:self.bottomToolbar setBarStyle:MKBarStyleWhite];
self.bottomToolbar.items=@[];
[self.view addSubview:self.bottomToolbar];


When I need to update it I'm calling (actually updating only one of its items but this does not matter):



-(void)updateToolbar
{
UIBarButtonItem *doneBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"done" style:UIBarButtonItemStylePlain target:self action:@selector(backButtonDidPress:)];
doneBarButtonItem.tintColor=MK_Color_Green;
UIBarButtonItem *clearBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"clear" style:UIBarButtonItemStylePlain target:self action:@selector(clearButtonDidPress:)];
clearBarButtonItem.tintColor=MK_Color_Pink;
UIBarButtonItem *spaceBetweenBackAndQuantity=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *spaceBetweenQuantityAndClear=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

UIBarButtonItem *quantityItem=[self quantityBarButtonItem];
self.bottomToolbar.items=[NSArray arrayWithObjects:doneBarButtonItem, spaceBetweenBackAndQuantity, quantityItem, spaceBetweenQuantityAndClear, clearBarButtonItem, nil];

[self.rootViewController reloadToolbar];
}


Both UIBarButtonItem objects that was created using initWithTitle:.. have negative left and right paddings somehow. It can be roughly fixed by adding additional fixed space type UIBarButtonItems to both sides of the items array. In other words: compensate strange initial left and right paddings with fixed space type UIBarButtonItems.


Help me someone, please. What causes such strange effect? How can I get rid of this strange padding?


Here's how it looks like:


strange initial toolbar negative padding




Aucun commentaire:

Enregistrer un commentaire