Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show button in center of toolbar?

I want to show button on top of and in center of Toolbar. I set the image of UIBarButtonItem but it does not give desired result as given in below image.

I am able to add Menu and Locate Me

but could not add Request Button as shown in pic.

Request BUtton is an image and when I set it the UIBarButtonItem image it gets stretched

please help

This is what I have done. enter image description here

But I want to do this. as You can see that in my solution Request button is under toolbar and in the below pic Request button is on top. enter image description here

like image 854
Azhar Avatar asked Jul 31 '13 11:07

Azhar


3 Answers

Swift version

    let flexibleSpace = UIBarButtonItem(
                        barButtonSystemItem: .flexibleSpace,
                        target: nil,
                        action: nil)

    let locateMe = UIBarButtonItem(
                        barButtonSystemItem: .plain,
                        target: self,
                        action: #selector(locateMe))

    let request = UIBarButtonItem(
                        barButtonSystemItem: .plain,
                        target: self,
                        action: #selector(request))

    let menu = UIBarButtonItem(
                        barButtonSystemItem: .plain,
                        target: self,
                        action: #selector(menu))

    let items = [locateMe, flexibleSpace, request, flexibleSpace, menu]

    setToolbarItems(items, animated: false)
like image 176
Mahbub Morshed Avatar answered Nov 13 '22 17:11

Mahbub Morshed


just add UIBarButtonItem with FlexibleSpace to both side of these two button like bellow..

UIBarButtonItem *flexibleSpace =  [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

See all code with two buttons..

UIBarButtonItem *flexibleSpace =  [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

UIBarButtonItem *locateMe = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStylePlain target:self action:@selector(locateMe:)];

UIBarButtonItem *request = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStylePlain target:self action:@selector(request:)];

UIBarButtonItem *menu = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStylePlain target:self action:@selector(menu:)];

NSArray *toolbarItems = [NSArray arrayWithObjects:locateMe,flexibleSpace,request , flexibleSpace, menu];

[toolBar setItems:toolbarItems animated:NO];

From XIB you can also set this UIBarButtonSystemItemFlexibleSpace like in bellow image..

enter image description here

UPDATE: you want to Add that request button with that image on UIToolBar then just bring that UIImageView and button after add UIToolBar on the UIView like bellow..

[self.view bringSubviewToFront:imgView];
[self.view bringSubviewToFront:btnRequest];
like image 21
Paras Joshi Avatar answered Nov 13 '22 18:11

Paras Joshi


From Nib use flexible space item on a UIToolbar object. like bellow Image:-

enter image description here

For Adding center Button Bigger then ToolBar Hight then check this bellow Demo and use Its' category for doing this Task just Drag you image into demo and change image name to CenterButtonIconPaw.png and check:-

https://github.com/el-eleven/EEToolbarCenterButton

using your image i done as you want It's Look like similar to your Need:-

enter image description here

like image 3
Nitin Gohel Avatar answered Nov 13 '22 18:11

Nitin Gohel