I want to change the title of post buttons in SLComposeServiceViewController.
I managed to get the UIButton:
NSArray* subviews =[self.navigationController.navigationBar subviews];
UIButton* postButton =[subviews lastObject];
and i tried to set title like this:
[postButton setTitle:#"Save" forState:UIControlStateNormal];
but the title not changed.
Can anyone help me with this?
I saw Evernote's share extension on my iPad and it looks like this:
UPDATE
My Solution:
I found solution for my question,I removed the original navigation bar and create custom nav bar.
I have two nav bar:
1. with "cancel"\"save" buttons
2. with "back" button
and I change them when navigate to other viewcontroller
(in my case I needed to upload file and user need select location from list)
NOTE: if you not implement configurationItems you need only the first nav bar. (just call to set custom nav bar from viewDidAppear
So my code is here:
#property (strong, nonatomic) UINavigationBar *customNavBar;
-(void) viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.customNavBar = [[UINavigationBar alloc] initWithFrame:self.navigationController.navigationBar.bounds];
[self.navigationController.navigationBar removeFromSuperview];
[self.navigationController.view addSubview:self.customNavBar];
[self setCancelSaveNavigationItem];
}
setCancelSaveNavigationItem--> called from viewDidAppear of shareViewController
-(void)setCancelSaveNavigationItem
{
UINavigationItem *newItem = [[UINavigationItem alloc] init];
UIBarButtonItem *cancelBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"Cancel",nil) style:UIBarButtonItemStylePlain target:self action:#selector(cancelButtonTapped:)];
UIBarButtonItem *saveBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"Done",nil) style:UIBarButtonItemStyleDone target:self action:#selector(saveButtonTapped:)];
newItem.leftBarButtonItem = cancelBarButtonItem;
newItem.rightBarButtonItem = saveBarButtonItem;
[self.customNavBar setItems:#[newItem]];
[self.navigationItem setBackBarButtonItem:cancelBarButtonItem];
[self.navigationItem setRightBarButtonItem:saveBarButtonItem];
if(self.item.value == nil){
saveBarButtonItem.enabled = NO;
}
}
setBackNavigationItem--> called in configurationItems -->in self.item.tapHandler function
-(void)setBackNavigationItem
{
UINavigationItem *newItem = [[UINavigationItem alloc] init];
UIBarButtonItem *selectBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"Select",nil) style:UIBarButtonItemStylePlain target:self action:#selector(selectButtonTapped:)];
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:#"❮ %#", NSLocalizedString(#"Back",nil)] style:UIBarButtonItemStylePlain target:self action:#selector(backButtonTapped:)];
newItem.leftBarButtonItem = backBarButtonItem;
newItem.rightBarButtonItem = selectBarButtonItem;
[self.customNavBar setItems:#[newItem]];
[self.navigationItem setBackBarButtonItem:backBarButtonItem];
[self.navigationItem setRightBarButtonItem:selectBarButtonItem];
}
Handle buttons tapped:
- (void)backButtonTapped:(id)sender {
if([self.navigationController.viewControllers count] ==2){
[self setCancelSaveNavigationItem];
}
[self.navigationController popViewControllerAnimated:YES];
}
- (void)cancelButtonTapped:(id)sender {
[self cancel];
}
- (void)selectButtonTapped:(id)sender {
...
[self setCancelSaveNavigationItem];
[self popConfigurationViewController];
}
- (void)saveButtonTapped:(id)sender {
...
[self cancel];
}
And it's work for me!!!
The result:
Simply in viewDidAppear
self.navigationController?.navigationBar.topItem?.rightBarButtonItem?.title = "Save"
Your code:
NSArray* subviews =[self.navigationController.navigationBar subviews];
UIButton* postButton =[subviews lastObject];
...is a really bad idea. It only works because the post button is in subviews and is the last item in the array. But the content of subviews is undocumented and might change at any time. Also, since there's no public API for this button, it's entirely possible that there's framework code to prevent or override changes to the button text-- so even if we assume you have the right UI element, you still might not be able to change it.
Evernote's UI is almost certainly a full custom design that only resembles SLComposeServiceViewController. Share extensions are not required to use SLComposeServiceViewController, that's just there for convenience. If it doesn't meet your needs, design your own.
Update: out of curiosity I unzipped the Evernote IPA and had a look at EvernoteShare.appex with nm. There's no reference to SLComposeServiceViewController, which confirms that Evernote is not using that class in their extension.
Related
I want to override to my own navigation controller with its own back button, title etc. But after i pushed the view controller, by default shows the navigation bar of the last view in the stack which was inherited in previous class.
Lets assume this is my first class which inherits NavBarViewController in header file as;
//ListingDetailViewController.h
#interface ListingDetailViewController : NavBarViewController
and, inside ListingDetailViewController, i pushed view by:
//ListingDetailViewController.m
ChatListViewController *chatList= [[ChatListViewController alloc]init];
[self.navigationController pushViewController:chatList animated:YES];
So this will still bring in the navigation controller same as in ListingDetailViewController, even though i tried to override the back button, title again in ChatListViewController's viewdidLoad and viewWillAppear. This is ChatListViewController's header:
//ChatListViewController.h
#interface ChatListViewController : IPChatListViewController
How can i achieve this? Any help is much appreciated. Thank you.
I use a BaseViewController class for this purpose. Paste this code in your base controller:
if (self.navigationController.viewControllers)
{
if (self.navigationController.viewControllers.count > 1)
{
[self createBackButton];
}
else
{
//[self createSideBarButton];
}
}
else
{
//[self createSideBarButton];
}
Include this method in your base class:
- (UIBarButtonItem *)createBackButton
{
UIBarButtonItem *negativeSeperator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSeperator.width = 0;
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"navigation_back_button.png"] style:UIBarButtonItemStylePlain target:self.navigationController action:#selector(popViewControllerAnimated:)];
[item setTintColor:[UIColor whiteColor]];
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSeperator,item, nil];
return item;
}
If you make sure you subclass your View Controllers (not navigation controller) from this base VC it will automatically replace back buttons.
I found the solution by overriding the button in viewDidAppear, because until viewDidLoad it will re-use the inherited navigation bar. So,
-(void)viewDidAppear:(BOOL)animated {
[self overwriteBackButton];
}
Then:
- (void)overwriteBackButton {
UIImage *backNormal = [UIImage imageNamed:#"backButton"];
_navBarLeftButton = [OpenSansButton buttonWithType:UIButtonTypeCustom];
_navBarLeftButton.frame = CGRectMake(10, 10, 35, 35);
[_navBarLeftButton setImage:backNormal forState:UIControlStateNormal];
[_navBarLeftButton addTarget:self action:#selector(backToList) forControlEvents:UIControlEventTouchUpInside];
[self.navigationController.navigationBar addSubview:_navBarLeftButton];
}
And it is triggered to:
-(void)backToList
{
[self.navigationController popViewControllerAnimated:YES];
}
I have a "LoginViewController" which presents a new Controller which is a subclass of UINavigationcontroller when clicking a button:
MPNavigationViewController *controller = [[MPNavigationViewController alloc] initWithRootViewController:[[MPQuestionFirstViewController alloc] init]];
[self presentViewController: controller animated:YES completion:nil];
"MPNavigationViewController" subclass UINavigationController and uses "REMenu" to have a sliding-from-top menu ("Link") and on viewDidLoad I try to add a right button to open it:
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *toggleMenuButton = [[UIBarButtonItem alloc] initWithTitle:#"Show" style:UIBarButtonItemStylePlain target:self action:#selector(toggleMenu:)];
self.navigationItem.rightBarButtonItem = toggleMenuButton;
[self initMenu];
}
It doesn't show any button on the navigation bar. Why could it be?
If I try to add the button from one of the "viewControllers" that will handle sections on the menu. It shows the button, but it doesn't paint it at all.
Thanks.
You are using subclass of UINavigationcontroller which is not actually view controller.
There is only one solution, You need to create your custom button and add it to UINavigationbar as a subview..
Use this hope it will help.
UIBarButtonItem *doneButton =[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(goToDoneButtonAction)];
self.navigationItem.rightBarButtonItem = doneButton;
**Background** When my app loads a UINavigationController is programmatically added and given a leftBarButtonItem and a rightBarButtonItem. During launch a check is made to see if new data is required and if so a method is run to go and get new data from our web server. During this time, the rightBarButtonItem is changed to a UIActivityIndicatorView which will show a spinning activity indicator. When the data download is done, the rightBarButtonItem returns to a UIBarButtonItem. This works fine.
On another UIViewController I need to allow the user to manually get new data and send their data to us. During this time I need the rightBarButtonItem to change to a UIActivityIndicatorView as before. But when I try to do so, the data is downloaded ok but the rightBarButtonItem is never changed.
I think my problem may have something to do with me not being able to reference the UINavigationController and its navigationItems.
**Code** My first UIViewController's implementation where the rightBarButtonItem is changed:
-(void)statusIsLoading{
UIActivityIndicatorView *uiLoading = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
uiLoading.hidesWhenStopped = YES;
NSLog(#"Start animating");
[uiLoading startAnimating];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:uiLoading];
}
-(void)statusFinishedLoading{
UIActivityIndicatorView *uiLoading = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
uiLoading.hidesWhenStopped = YES;
[uiLoading stopAnimating];
UIBarButtonItem *showSettingsButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"Settings.png"] style:UIBarButtonItemStylePlain target:self action:#selector(showSettings:)];
self.navigationItem.rightBarButtonItem = showSettingsButton;
}
**What I have tried** I have tried setting the self.navigationItem.rightBarButtonItem in the second UIViewController but this does nothing. I have also tried running the method that calls the above two methods from an instantiated class:
InitViewController *ivcInstance = [[InitViewController alloc] init];
[ivcInstance performUpdate];
This runs the 'performUpdate' code just fine, the 'performUpdate' method called the 'statusIsLoading' and 'statusFinishedLoading' methods but the rightBarButtonItem does not change.
I think there is something very simple I am missing here. Thanks.
**Edit** I have just tried checking for the current value of the rightBarButtonItem on the pageLoad of the second UIViewController and it appears to be nil:
UIBarButtonItem *newItem = self.navigationItem.rightBarButtonItem;
Here newItem = nil. Am I not targeting the correct navigationItem? should it be something other than 'self'?
**Edit 2**
In the second UIViewController I can alter the properties of the navigationBar:
[self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:0.6 green:0.01 blue:0.36 alpha:2.0f]];
But if I try the same with the barButtonItem then nothing happens, why is this?
[self.navigationController.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:uiLoading]];
same with:
[self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:uiLoading]];
In your statusFinishedLoading you have some unused codes like below
-(void)statusFinishedLoading
{
/*UIActivityIndicatorView *uiLoading = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
uiLoading.hidesWhenStopped = YES;
[uiLoading stopAnimating];*/ //These are unused codes.
UIBarButtonItem *showSettingsButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"Settings.png"] style:UIBarButtonItemStylePlain target:self action:#selector(showSettings:)];
self.navigationItem.rightBarButtonItem = showSettingsButton;
}
Also put NSLog in statusFinishedLoading methods and check whether this method called or not.
Thanks!
Try to set the rightBarButtonItem within a main-thread block. Once you have received the response, it should not block the UI.
-(void)statusFinishedLoading
{
UIActivityIndicatorView *uiLoading = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
uiLoading.hidesWhenStopped = YES;
[uiLoading stopAnimating];
UIBarButtonItem *showSettingsButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"Settings.png"] style:UIBarButtonItemStylePlain target:self action:#selector(showSettings:)];
//Use a block to set the rightBarButton.
dispatch_async(dispatch_get_main_queue(), ^{
self.navigationItem.rightBarButtonItem = showSettingsButton;
});
}
Hope this does the trick.
Edit : You need to create a UINavgationitem in the storyboard, connect the outlet to the .h file and call it statusNavigationItem then reference this navigationItem object to set the rightBarButton.
I'm trying to add a UISegmentedControl programmatically as the titleView in a UINavigationController. But it did not show up. Upon further investigation, I found out that the titleView property is ignored if the leftBarButtonItem is not set to nil according to the Apple docs. So I set it to nil but still the segmented control does not show up!
Below is my code.
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = nil;
UISegmentedControl *statFilter = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Personnal", #"Department", #"Company", nil]];
[statFilter setSegmentedControlStyle:UISegmentedControlStyleBar];
self.navigationItem.titleView = statFilter;
}
What should I do? What am I missing here?
Thanks.
EDIT :
Thanks for all the responses. However, none of them worked for me so I took a deeper look. It must be the way I have laid out my application. I probably should have mentioned that earlier. Apologies.
What I'm creating is a Storyboard app. The main controller is a UITabBarController. When a certain tab is clicked, I want to show a UITableViewController. Since you cannot display a Navigation bar in a UITableViewController, I found out that I must add a UINavigationController and implement a UITableViewController inside it. This is how my app basically look like,
That Dashboard View Controller is connected to a class inheriting from UINavigationController
class. Maybe this is where the issue resides?
Because I just created a small test app, put a UIViewController, embedded in a UINavigationController and wrote the code I have posted in my original question above inside the UIViewController's ViewDidLoad and the segmented control shows up just fine. Now I don't know how to do the same in my main app because there's no other view controllers or anything connected to the navigation controller.
Sorry if I made things even worse. Please comment if anything is unclear.
I guess you have to make the UISegmentedControll a UIBarButtonItem:
UPDATE:
This Code makes the SegmentedControll centered:
- (void)viewDidLoad{
self.title = nil;
UISegmentedControl *statFilter = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Personnal", #"Department", #"Company", nil]];
[statFilter setSegmentedControlStyle:UISegmentedControlStyleBar];
NSMutableArray *buttonArray = [[NSMutableArray alloc] init];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[buttonArray addObject:flexibleSpace];
[buttonArray addObject:[[UIBarButtonItem alloc] initWithCustomView:statFilter]];
[buttonArray addObject:flexibleSpace];
self.navigationItem.leftBarButtonItems = buttonArray;
[buttonArray release];
}
I hope this helps you out!
Use this i hope this will help you
- (void)viewDidLoad
{
[super viewDidLoad];
UIView * navview = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 160,80)];
self.navigationItem.leftBarButtonItem = nil;
UISegmentedControl *statFilter = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Personnal", #"Department", #"Company", nil]];
[navview addSubview:statFilter];
self.navigationItem.titleView = navview;
}
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] initWithTitle:#"Refresh" style:UIBarButtonItemStylePlain target:self action:#selector(refreshButtonClicked:)];
[self.navigationItem setRightBarButtonItem:refreshButton animated:YES];
}
- (void)refreshButtonClicked:(id)sender {
NSLog(#"Refreshed.");
}
I added a navigation controller programmatically in AppDelegate and set this view as the root.
When I was trying to add a navigationItem programmatically as shown above, the refreshButton showed in the navigation bar but refreshButtonClicked: was not invoked.
What's the problem? Any suggestions and comments are welcome.
Make a call to the super class in your viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] initWithTitle:#"Refresh" style:UIBarButtonItemStylePlain target:self action:#selector(refreshButtonClicked:)];
[self.navigationItem setRightBarButtonItem:refreshButton animated:YES];
}
It will ensure that the root class setup is done
Problem solved. Weird. After I clean the builds, delete the derived data and restart Xcode, it goes runs properly.