I have a uisegementcontrol and a uibarbutton that gets added to the navigation bar of the navigation controller. When I push to the next view, however, I want them to be removed. I tried several approaches, but nothing seems to be removing them. One approach was using viewWillDisappear and having the segementcontrol and the bar button removedFromSuperView. Another was to call the removeFromSuperView from the method that pushes to the next scene. Nothing seems to be working. I've actually been stuck on this for a really long time!
Also, the view controller "X" that has the segement control and the bar button is presented through a presentViewController. X then pushes the other view controller.
Here is the code for the segement control
NSArray *headers = #[#"Tracks", #"Playlists"];
self.savedMusic = [[UISegmentedControl alloc] initWithItems:headers];
CGSize size = self.view.frame.size;
CGRect segFrame = CGRectMake(80, 5, size.width - 140, 35);
self.savedMusic.backgroundColor = [UIColor blackColor];
self.savedMusic.frame = segFrame;
self.savedMusic.tag =1;
[self.savedMusic setTitle:#"Tracks"
forSegmentAtIndex:0];
[self.savedMusic setTitle:#"Playlists"
forSegmentAtIndex:1];
[self.savedMusic addTarget:self
action:#selector(whatToDisplay)
forControlEvents:UIControlEventValueChanged];
self.savedMusic.selectedSegmentIndex = 0;
[self.navigationController.navigationBar addSubview:self.savedMusic];
Here is the code for the viewWillDisapear
[super viewWillDisappear:animated];
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
self.navigationItem.leftBarButtonItem = nil;
[self.savedMusic removeFromSuperview];
And here is the code for the method that switches to the new view
PlaylistTracksTableViewController *vc = [[PlaylistTracksTableViewController alloc] init];
[self.savedMusic removeFromSuperview];
[[self.navigationController.navigationBar viewWithTag:1] removeFromSuperview];
self.navigationItem.leftBarButtonItem = nil;
[self.navigationController pushViewController:vc animated:YES];
The problem is that what you are doing is not how you manipulate the contents of the navigation bar. Do not talk to self.navigationController.navigationBar. Instead, each view controller should set its own self.navigationItem, and the runtime will use that to populate the navigation bar for that view controller.
Related
My app looks like this when you launch it:
On every row except for the top one, I can push a new view controller, and dismiss it, and everything stays the same. However, if I click the Instagram row, and then dismiss the view that is pushed onto it, my screen looks like this:
In the Instagram controller that is pushed, the viewDidLoad looks like this:
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"Instagram";
UIBarButtonItem *theButton = [[UIBarButtonItem alloc] initWithTitle:#"Post" style:UIBarButtonItemStylePlain target:self action:#selector(doit)];
self.navigationItem.rightBarButtonItem = theButton;
self.downloading = YES;
self.mediaArray = [NSMutableArray arrayWithCapacity:0];
[self.collectionView registerClass:[SBInstagramCell class] forCellWithReuseIdentifier:#"SBInstagramCell"];
[self.collectionView setBackgroundColor:[UIColor whiteColor]];
[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:#"footer"];
[self.navigationController.navigationBar setTranslucent:NO];
[self.view setBackgroundColor:[UIColor whiteColor]];
self.multipleLastEntities = [NSArray array];
self.instagramController = [SBInstagramController instagramControllerWithMainViewController:self];
self.instagramController.isSearchByTag = self.isSearchByTag;
self.instagramController.searchTag = self.searchTag;
[self downloadNext];
self.collectionView.alwaysBounceVertical = YES;
refreshControl_ = [[SBInstagramRefreshControl alloc] initInScrollView:self.collectionView];
[refreshControl_ addTarget:self action:#selector(refreshCollection:) forControlEvents:UIControlEventValueChanged];
loaded_ = YES;
[self showSwitch];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
}
I get to this view like this:
SBInstagramController *instagram = [SBInstagramController instagram];
instagram.showOnePicturePerRow = YES; //to change way to show the feed, one picture per row(default = NO)
instagram.showSwitchModeView = YES; //show a segment controller with view option
[instagram refreshCollection]; //refresh instagram feed
[self.navigationController pushViewController:instagram.feed animated:YES];
To me, it looks like the UIImageView is being shifted down about 44 pixels, which would be the space of a Navigation Bar, I just can't understand why it would be doing that. I use AutoLayout and have both the UITableView and the UIImageView pinned to the top superview, but it is only the UIImageView that moves.
How can I do something like this:
I tried to changed through setframe and nothing happened:
UIImage *flechSuiv = [UIImage imageNamed:#"picto_fleche_lieux_suiv.png"];
UIButton *buttonSuiv = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonSuiv setImage:flechSuiv forState:UIControlStateNormal];
[buttonSuiv addTarget:self action:#selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];
[buttonSuiv setFrame:CGRectMake(0, 34+flechSuiv.size.width, flechSuiv.size.width,flechSuiv.size.height)];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:barButtonSuiv,barButtonPrec,nil];
self.navigationController.navigationBar.translucent = NO;
I think you should go for a custom navigation bar. You'd need to hide the navigation bar before presenting the view.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:NO];
}
And put an UIView matching the size of a regular tab bar in your view controller.
Inside of that view, you can put any custom buttons with regular action (push or pop).
adding a button to navigation bar on first nib viewController and wanted to view this button to only on main screen not on any other screen
i have worked out on it
i made the button and added to subview of navigation bar
on every button action i have put this
- (IBAction)forth:(id)sender {
forthView *forthview = [[forthView alloc]init];
[self.navigationController pushViewController:forthview animated:YES];
btn.hidden = YES;
}
after this button hides but didnot show up when i got back to main screen
my code for viewdidload is here
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.title = #"My First View";
self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
btn = [[UIButton alloc] init];
btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(10, 10, 65, 30);
[btn setTitle:#"Show" forState:UIControlStateNormal];
[self.navigationController.navigationBar addSubview:btn];
btn.hidden = false;
}
Once you hide the button so you need to unhide it again. here is the simple code to unhide the button:
btn.hidden = No
viewDidLoadis only called the first time your view is loaded. Add an NSLog()statement if you want to test when it gets called. Every time your view appers on screen viewWillAppearis called.
So you need to show the button in viewWillAppear
It seems likely that what you really want to be doing is adding a UIBarButtonItem to your main view controller's navigationItem.leftBarButtonItem property. That way the button will have the correct appearance and show and hide as the view controller is displayed.
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Show"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(onShowButtonPressed:)];
I wish to add a UIToolbar programmatically to a view when a user clicks on a button (in my case when they zoom in on a photo).
It seems to work fine when I create the toolbar in the click method and add to the subview but if I create the toolbar in the viewDidLoad method, assign it to an instance variable,and add that instance variable later to the subview on click, nothing appears. The debugger shows that the instance variable is a UIToolbar and is not null. I didn't want to create and destroy the same toolbar on every click so I thought it was better just to keep it as an instance variable that I add and remove from the view as needed. Is this the right approach?
Why is it visible in the one case and not the other.
Setup
#synthesize toolBar;
- (UIToolbar*)createToolbar
{
UIToolbar* toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, self.view.frame.size.height - 44, self.view.frame.size.width, 44);
UIBarButtonItem *shareButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(sharePhoto:)];
NSArray *buttonItems = [NSArray arrayWithObjects:shareButton,nil];
[toolbar setItems:buttonItems];
return toolbar;
}
This works
- (void) clickMyButton {
toolBar = [self createToolbar];
[self.view addSubview:toolBar];
}
This doesn't show anything
- (void)viewDidLoad
{
[super viewDidLoad];
toolBar = [self createToolbar];
}
- (void) clickMyButton {
[self.view addSubview:toolBar];
}
Why doesn't it work in the latter case
The problem is that when viewDidLoad gets called, it is not guaranteed that the frames for your view and subviews are set. Try calling [self createToolbar] from viewWillAppear or viewDidAppear instead.
When initiating a view from a UIActionSheet button, upon returning to the view via the navigationBar back button, the toolbar while still visible does not have any of the buttons that were previously on it. This error has arisen since updating to iOS 6 and occurs while testing it on the simulator and a device running iOS 6 only. If I comment out the code that hides the toolbar on the view pushed by the UIActionSheet the buttons are added when going back.
I'm making my toolbar items programatically in viewWillAppear and showing the UIActionSheet from the toolbar which I'm accessing via self.navigationController.toolbar.
Any idea what is causing this problem? It's only happened since iOS 6 has come around so is there any changes that I need to take into account regarding viewWillAppear?
This is how the view is pushed from the actionSheet:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (actionSheet.tag == 2) {
if (buttonIndex == 0) {
[self dismissAllTips];
self.actionNoteAddView= [[self.storyboard instantiateViewControllerWithIdentifier:#"IDActionNoteAddView"] retain];
actionNoteAddView.note_id = 0;
actionNoteAddView.iscompleted=0;
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:#"tool_tips"];
[self.navigationController pushViewController:actionNoteAddView animated:TRUE];
[actionNoteAddView release];
}else if(buttonIndex == 1){
...
These are the view methods for the pushed view:
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.navigationItem.hidesBackButton = NO;
[self.navigationController setToolbarHidden:YES];
txtcontent.layer.cornerRadius = 10.0f;
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
}
These are the view methods for the view that pushed the view using the actionSheet:
- (void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.navigationItem.hidesBackButton = YES;
[self.navigationController setToolbarHidden:NO];
self.navigationController.navigationBarHidden=NO;
self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.8 green:0.45 blue:0.2 alpha:1];
self.navigationController.toolbar.barStyle = UIBarStyleBlackOpaque;
self.navigationController.toolbar.tintColor = [UIColor colorWithRed:0.8 green:0.45 blue:0.2 alpha:1];
UIImage *actionButtonImage = [UIImage imageNamed:#"31-circle-plus#2x.png"];
UIBarButtonItem *actionButton = [[UIBarButtonItem alloc] initWithImage:actionButtonImage style:UIBarButtonItemStylePlain target:self action:#selector(actionPressed:)
];
UIImage *dashButtonImage = [UIImage imageNamed:#"19-gear.png"];
UIBarButtonItem *dashButton = [[UIBarButtonItem alloc] initWithImage:dashButtonImage style:UIBarButtonItemStylePlain target:self action:#selector(settingsPressed:)];
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
NSArray *toolitems = [NSArray arrayWithObjects:dashButton, flexItem, actionButton, flexItem, nil];
[self setToolbarItems:toolitems];
self.title = #"Dashboard";
defaultProfile.text = [[NSUserDefaults standardUserDefaults] stringForKey:#"default_profile"];
BOOL dailyProcess = [[NSUserDefaults standardUserDefaults] boolForKey:#"daily_process"];
if(dailyProcess){
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:#"daily_process"];
[[NSUserDefaults standardUserDefaults] synchronize];
loading = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
labelProcess = [[UILabel alloc]initWithFrame:CGRectMake(60, 105, 240, 30)];
labelProcess.text = #"Processing...";
labelProcess.backgroundColor = [UIColor clearColor];
labelProcess.textColor=[UIColor colorWithRed:0.8 green:0.45 blue:0.2 alpha:1];
[labelProcess setFont:[UIFont systemFontOfSize:20]];
loading.opaque = NO;
loading.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.6f];
indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[indicator setHidesWhenStopped:YES];
indicator.center = self.view.center;
[self.view addSubview:loading];
[self.view addSubview:indicator];
[self.view addSubview:labelProcess];
[indicator startAnimating];
}
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
CGRect frame = CGRectMake(157, 365, 10, 10);
UIView *viewToPointAt = [[UIView alloc] initWithFrame:frame];
[self.view addSubview:viewToPointAt];
BOOL willies = [[NSUserDefaults standardUserDefaults] boolForKey:#"tool_tips"];
if(willies==YES){
if(popTip == nil) {
popTip = [[[CMPopTipView alloc] initWithMessage:#"Step 1/3: This is the Action Button. You can create, view and auto-fill notes which are then added to your timeline.(Click for step 2)."] autorelease];
popTip.delegate = self;
[popTip presentPointingAtView:viewToPointAt inView:self.view animated:YES];
popTip.backgroundColor = [UIColor colorWithRed:0.8 green:0.45 blue:0.2 alpha:1];
popTip.textColor = [UIColor whiteColor];
}
}
[viewToPointAt release];
}
Tried a lot of different techniques and eventually sorted it by showing it in one view method and hiding it in the next. It's one fo the strangest bugs I have encountered and this is hardly a fix, but I hate having unanswered questions.
You may solve this by hiding the toolbar in the next view, but that is not a very good solution, as other views that can be possibly opened from the view that contains the toolbar will all have to hide the toolbar when needed, this is annoying.
Another solution is to hide the toolbar in viewDidDisappear but there is another problem, let's say another new view needs the toolbar as well, and it sets the toolbar visible in its own viewWillAppear then the problem is that the viewWillAppear for the new view will be called actually before viewDidDisappear of the previous view, so in that case the toolbar disappear even the new view desires it.
Anyway, I'm not sure if this is a bug of iOS6 as it works fine for iOS5, what worked for me was to wrap the code that opens the new view into dispatch_async(dispatch_queue_t queue, dispatch_block_t block), my understanding is that by doing this the action sheet will be dismissed before the new view is shown as you put the code that opens the new view to the end of the main queue(like what we did for viewDidDisappear, but this happens before the call to viewWillAppear of next view so it works perfect).
Got the solution, and I think it is an Apple bug in iOS 6.0 and above.
In the NavigationController view stack if any view hides the toolbar, e.g. self.navigationController.toolbarHidden = YES, then since that point on all the Views that had already created a toolBar with buttons, will loose the buttons.
So, my solution to this problem was to keep the toolBar in all the Views(had to compromise on the UI, but in my app pikSpeak, the functionality was very important.)
This is an ancient thread, I know, but I just recently ran into this problem. The clue to my solution was that some delay was needed between pushing a new view and closing the action sheet. I changed from using clickedButtonAtIndex to didDismissWithButtonIndex so that the push would occur after the action sheet was gone. Problem solved!