How to distribute buttons by length in UIToolbar?

In my application, I use some code to dynamically add image buttons to UIToolbar :

 [self.navigationController setToolbarHidden:NO]; UIImage *buttonImage1 = [UIImage imageNamed:@"1.png"]; UIImage *buttonImage2 = [UIImage imageNamed:@"2.png"]; UIImage *buttonImage3 = [UIImage imageNamed:@"3.png"]; UIBarButtonItem *toolButton1 = [[UIBarButtonItem alloc] initWithImage:buttonImage1 style:UIBarButtonItemStylePlain target:self action:@selector(btnSettingsClick:)]; UIBarButtonItem *toolButton2= [[UIBarButtonItem alloc] initWithImage:buttonImage2 style:UIBarButtonItemStylePlain target:self action:@selector(btnSettingsClick:)]; UIBarButtonItem *toolButton3 = [[UIBarButtonItem alloc] initWithImage:buttonImage3 style:UIBarButtonItemStylePlain target:self action:@selector(btnSettingsClick:)]; [self setToolbarItems:[NSArray arrayWithObjects:toolButton1, toolButton2, toolButton3, nil]]; 

But this does not work:

screenshot

If I try to set a different button style:

 toolButton1.style = UIBarButtonSystemItemFlexibleSpace; toolButton2.style = UIBarButtonSystemItemFlexibleSpace; toolButton3.style = UIBarButtonSystemItemFlexibleSpace; 

It also looks bad:

screenshot 2

How can i fix this?

+7
source share
3 answers

Add two additional button buttons that use the UIBarButtonSystemItemFlexibleSpace system style and place them between each of the existing buttons:

 [self.navigationController setToolbarHidden:NO]; UIImage *buttonImage1 = [UIImage imageNamed:@"1"]; UIImage *buttonImage2 = [UIImage imageNamed:@"2"]; UIImage *buttonImage3 = [UIImage imageNamed:@"3"]; UIBarButtonItem *toolButton1 = [[UIBarButtonItem alloc] initWithImage:buttonImage1 style:UIBarButtonItemStylePlain target:self action:@selector(btnSettingsClick:)]; UIBarButtonItem *toolButton2= [[UIBarButtonItem alloc] initWithImage:buttonImage2 style:UIBarButtonItemStylePlain target:self action:@selector(btnSettingsClick:)]; UIBarButtonItem *toolButton3 = [[UIBarButtonItem alloc] initWithImage:buttonImage3 style:UIBarButtonItemStylePlain target:self action:@selector(btnSettingsClick:)]; [self setToolbarItems:[NSArray arrayWithObjects: toolButton1, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], toolButton2, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], toolButton3, nil]]; 

This is strange for conceptualization, but flexible space is actually a separate object, not a style to apply to other objects.

+10
source

You need to use flexible spaces to distribute the buttons along the length of the toolbar. Before the buttons, each button, and after your buttons you should have a flexible space bar. (UIBarButtonSystemItemFlexibleSpace)

 UIBarButtonItem *flexibleSpaceBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL] 

The toolbar item should look something like this:

 [self setToolbarItems:[NSArray arrayWithObjects:toolButton1, flexibleSpaceBtn1, toolButton2, flexibleSpaceBtn2, toolButton3, nil]]; 
+3
source

Here is the link code I'm using -

 //Constants let imageNames : [String] = ["img1.png", ... "imgN.png"]; /********************************************************************************/ /** @fcn spacingDemo() * @brief space all toolbar items evenly across the UIToolbar on keyboard * * @param [in] (UITextView) textView : view to attach keyboard to in response */ /********************************************************************************/ func spacingDemo() { //Vars var barButtons : [UIBarButtonItem]; var button : UIButton; var img : UIImage; //Init keyboardToolbar = UIToolbar(); barButtons = [UIBarButtonItem](); //Config keyboardToolbar.barTintColor = UIColor.lightGray; /* set bkgnd color */ let flexBarButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil); barButtons.append(flexBarButton); /* size-to-fit */ for imageName in imageNames { //Gen Button button = UIButton(type: .custom); button.setImage(UIImage(named: imageName), for: .normal); button.addTarget(self, action: #selector(self.keyboardResponse), for: .touchUpInside); barButtons.append(UIBarButtonItem(customView: button)); //Apply Spacing let flexBarButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil); barButtons.append(flexBarButton); /* size-to-fit */ } //Assemble keyboardToolbar.items = barButtons; //Attach textView.inputAccessoryView = keyboardToolbar; //Cleanup keyboardToolbar.sizeToFit(); return; } 
0
source

All Articles