Progmatically change UIBarButtonItem identifier on UINavigationBar - ios

So I am just using a normal View Controller. I dragged a Navigation bar to the top and added a UIBarButtonItem with the 'pause' identifier.
I want to be able to change that identifier to the 'play' one.
Is this possible? I can't find any information on it.
I tried creating a new one, but not really sure where to go from there.
UIBarButtonItem *flipButton = [[UIBarButtonItem alloc]
initWithTitle:#"Flip"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(flipView)];
Any help would be great.

You're looking for the UIBarButtonSystemItemPlay. Just set it up, assign your selector and target, and choose which barButtonItem you want.
UIBarButtonItem *playButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:#selector(playButtonPressed:)];
self.navigationItem.rightBarButtonItem = playButton;//Or leftBarButtonItem if you want it to be on the left

Related

Is it possible to add a UIBarButtonItem besides a UISegmentedControl?

I've added a UISegmentedControl to a UINavigation Bar, and when I tried to add a UIBarButtonItem programmatically in viewDidLoad viewController using the following code:
UIBarButtonItem *timeTableBarButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"blueRoundRect"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(popUpTimeTable)];
self.navigationItem.rightBarButtonItem = timeTableBarButton;
There are no button showing at all. I wonder since I already have a UISegmentedControl, is it possible to do so?
It is possible to add the Bar button in navigation bar and there is no way your segment control should be blocking right bar button item. Please make sure that your self.navigationItem is not nil.

How to set the navigation bar button when user taps “More”on UIActivityViewController?

Whenever the user wants to select a new share method or action that isn't listed by default, by tapping the "More" button on the UIActivityViewController generated share sheet, a new view is displayed, As everybody knows.i need to change the bar button on that page
can anybody help me on adding our own bar button as a rightbarbutton item?
Try with this. if not you want this ,then explain a little bit more.
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(doneButtonPressed)];
self.navigationItem.rightBarButtonItem = addButton;

How to change the backButtonItem title on a UIViewController?

I feel daft asking this question, but I haven't been able to get this to work. I have a NavController->TableViewController->WebViewController. I'm trying to change the title of the backButton on the WVC from the default "Back" to "Reference".
This should be a simple matter of setting the text property of the Back button either in Storyboard (which isn't working) or in code of my WebViewController's viewDidLoad method:
self.navigationItem.backBarButtonItem.title = #"Reference";
Is this just a bug or am I missing something?
Put this in your tableviewController
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Reference" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
Screen shot
From document
If you want to specify a custom image or title for the back button, you can assign a custom bar button item (with your custom title or image) to this property instead.
You should set it in your TableViewController, not in your WebViewContorller
Solved: I had added a grey activity indicator in a small view in the nav bar of the WVC. After I removed it, the code worked fine. Thanks #Leo, #Jacky, #Jaleel!
this work for me
self.navigationController.navigationBar.topItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Reference" style:UIBarButtonItemStylePlain target:nil action:nil];

How to align UIBarButtonItem in UIToolbar in iOS universal app?

I want to put two UIBarButtonItem instances to UIToolbar in my iOS universal app, on which the first item is on the left-edge of the toolbar and the second is on the center. However, it looks like this cannot be done in Interface Builder, because the auto layout doesn't support forcing constraints on UIBarButtonItem class due to the fact that it is not a subclass from UIView.
Notice that I don't want to put the first on the left-edge and the second is on the right-edge - I have to put the second button at the center. Also, I don't have a plan to use the third button in order to align them as left, center, and right at equal interval.
Is it still possible to add such constraint in this situation in storyboard? I use Xcode 6 beta 5.
You can do it all in your Storyboard.
Select a Bar Button Item in the Object library and drag it into your Toolbar. Add a Flexible Space Bar Button Item at its right. Add your second Bar Button Item at its right. Then, add a second Flexible Space Bar Button Item at its right.
The result will look like this in Interface Builder:
If necessary, you can add an extra Fixed Space Bar Button Item at the right edge of your Toolbar to be sure that your second Bar Button Item is really centered.
User lxt gives another example of this in answer for a similar question here.
Edit: Be sure that your UIToolbar has good constraints before proceeding!
you can do this programmatically
UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:#"item1" style:UIBarButtonItemStylePlain target:self action:nil];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:#"item2" style:UIBarButtonItemStylePlain target:self action:nil];
UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithTitle:#"item3" style:UIBarButtonItemStylePlain target:self action:nil];
self.toolbarItems = [NSArray arrayWithObjects: item1, flexible, item2, flexible, item3, nil];
check this link if you want more details
How to place UIBarButtonItem on the right side of a UIToolbar?
I think you should try Flexible bar button space item and for more understating i am sharing a link, so have look of it might this help you out
UIToolbar and Flexable/Fixed bar button items
Code to add toolbar :
Here fixed width can be variable. Accordingly you can keep it as far as you want so as to keep the second button in centre.
UIBarButtonItem *fixedItemSpaceWidth = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
fixedItemSpaceWidth.width = 200.0f; // or whatever you want
UIBarButtonItem *doneBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonAction:)];
UIBarButtonItem *cancelBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelButtonAction:)];
// Initialise toolabr
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-44, 320, 44)];
//toolbar.barStyle = UIBarStyleBlack;
toolbar.items = [NSArray arrayWithObjects:cancelBarButton,fixedItemSpaceWidth,doneBarButton, nil];
[self.view addSubview:toolbar];

Add goBack and goForward to the right of the Navigation Bar

I've a WebviewViewController and a navigation bar with the Back Bar Button. Now I want two buttons on the right of the bar, a goBack and a goForward. I've searching for a while and none of the answers works properly.
There's a way to add them in the storyboard or only programmatically? If so, how do I do it?
I did it in the past, it's easy:
myBackBarButton = [[UIBarButtonItem alloc] initWithImage:barButtonImage
style:UIBarButtonItemStylePlain
target:myWebView
action:#selector(goBack)];
myForwardButtom = [[UIBarButtonItem alloc] initWithImage:anotherBarButtonImage
style:UIBarButtonItemStylePlain
target:myWebView
action:#selector(goForward)];
Where myWebView is your UIWebView (In case it's your class, use self)
If your buttons are already created, then just add the target and the action to them:
myBackButton.target = myWebView;
myBackButton.action = #selector(goBack);
What you are doing is adding the action goForward: and goBack: of the target that is your webview, to those buttons.

Resources