View in popover is missing some, but not all, subviews - ios

I am trying to make a view reveal inside a popover. Something I have done numerous times. However, for some reason this time, the popover is appearing but missing some (but not all) of it subviews.
More specifically, this view contains a label, a text field, a button, and also has a navigation bar at the top. When revealed, it displays a label, but not the text field, or button. Ive tried with and without a XIB and results are the same.
Here is the declaration of the popover.. (triggers on button click)
if (popover)
{
[popover dismissPopoverAnimated:NO];
popover = nil;
}
// create the settings view controller and display it in a popover
LoginViewController *loginViewController = [[LoginViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginViewController];
// Initialize the popover, set the parent view as the delegate and allocate the popover's origin on the screen
popover = [[UIPopoverController alloc] initWithContentViewController:nav];
popover.delegate = self;
[popover presentPopoverFromRect:button.frame inView:button.superview permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
and in the view did load of the loginViewController I have
self.navigationItem.title = NSLocalizedString(#"POPOVER_TITLE_INFORMATION", nil);
// setup navigation buttons
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"BUTTON_TITLE_CANCEL", nil) style:UIBarButtonItemStyleBordered target:self action:#selector(cancel)];
UIBarButtonItem *searchButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"BUTTON_TITLE_SEARCH", nil) style:UIBarButtonItemStyleDone target:self action:#selector(search)];
self.navigationItem.rightBarButtonItem = searchButton;
self.navigationItem.leftBarButtonItem = cancelButton;
[_loginTextLabel setText:NSLocalizedString(#"DESCRIPTION_TEXT", nil)];
[_loginTextLabel setNumberOfLines:0];
_loginTextLabel.frame = CGRectMake(20, 30, 728, 40);
[self.view addSubview:_loginTextLabel];
_loginTextField.frame = CGRectMake(20, 80, 264, 30);
[self.view addSubview:_loginTextField];
[_loginSubmitButton setTitle:NSLocalizedString(#"BUTTON_TITLE_SUBMIT", nil) forState:UIControlStateNormal];
[_loginSubmitButton addTarget:self action:#selector(submitBtnClk:) forControlEvents:UIControlEventTouchUpInside];
[_loginSubmitButton setFrame:CGRectMake(20, 130, 208, 44)];
[self.view addSubview:_loginSubmitButton];
Nothing overly complex, however the label is the only thing that actually displays to the screen.

I found the solution.
It would seem that Popovers AutoResize their components, and the widths of the button and the textfield were not large enough. Essentially, the popover screen is smaller than a normal view covering the full window, so it was shrinking the widths of the textField and button all the way to 0 when i guess the popover was adjusting the view.
So basically, I had to jack up the widths of those subviews to compensate.

Related

UIPopoverPresentationController not showing rightBarButtonItems in iOS 11

I'm trying to show a UIPopoverPresentationController which has a UIButton in it's navigation bar. This used to work, but the UIButton is not showing anymore, since iOS 11 (iPad). Interestingly, in my popup, I can also push another UIViewController, and when I come back from it, the UIButton appears.
Here's the code to show the popup:
- (IBAction)buttonPressed:(id)sender {
PopupViewController *popupController = [self.storyboard instantiateViewControllerWithIdentifier:#"PopupController"];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:popupController];
navController.modalPresentationStyle = UIModalPresentationPopover;
[self presentViewController:navController animated:YES completion:nil];
UIPopoverPresentationController *popController = [popupController popoverPresentationController];
CGRect rect = self.button.frame;
CGSize size = CGSizeMake(500, 400);
popController.sourceView = self.view;
popController.sourceRect = rect;
popupController.preferredContentSize = size;
}
and here is the code in the popup, to show the UIButton:
- (void)viewWillAppear:(BOOL)animated
{
UIButton *rightButton = [[UIButton alloc]init];
[rightButton setTitle: #"Press me" forState:UIControlStateNormal];
[rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
rightButton.frame = CGRectMake(0, 0, 120, 24);
UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:flexible, rightButtonItem, nil];
self.navigationController.preferredContentSize = self.preferredContentSize;
}
Does anybody know what's going on? Why is the button now showing right away?
I would move your code out of viewWillAppear and into viewDidLoad. Its being in viewWillAppear is the reason you can see the button when you open another controller and then go back to this one, but you don't really want the button to be recreated every time you go back to this view. I think there's a good chance this will fix the problem of it not showing at first, too.

Can't set titleView in Navigation bar center and scope of back button increased

I am trying to push a view controller and set its navigation bar's title but it is not centered due to long title being used i guess. Along with that, scope of the back button is increased till the title view, i.e if I tap with with Milestone's "M", it gets back though it frame is the same.
Bounds of back button are the same but its click impact is elongated.
Below is the code for how I am pushing the view controller.
MilestoneDetailsViewController *controller = [[MilestoneDetailsViewController alloc] initWithNibName:#"MilestoneDetailsViewController" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
and in the MilestoneDetailsViewController, i set the title as following:
self.title = NSLocalizedString(#"Milestone Details", nil);
Back button is picking its size according to the title of previous viewController. you can change it to a empty String for example, in your previous viewController from where you are pushing write this code.
self.navigationItem.backBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:#""
style:UIBarButtonItemStylePlain
target:nil
action:nil];
Try this
UIView *tView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 160 ,44)];
UILabel *bartitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 4, tView.frame.size.width, 40)];
[bartitleLabel setBackgroundColor:[UIColor clearColor]];
[bartitleLabel setTextAlignment:NSTextAlignmentCenter];
[bartitleLabel setAdjustsFontSizeToFitWidth:YES];
[bartitleLabel setText:NSLocalizedString(#"Milestone Details!",#"")];
[self.navigationItem setTitle:#""];
[tView addSubview:bartitleLabel];
[tView setBackgroundColor:[UIColor clearColor]];
[self.navigationItem setTitleView:tView];
Try like this
UILabel* lbNavTitle = [[UILabel alloc] initWithFrame:CGRectMake(0,40,320,40)];
lbNavTitle.textAlignment = UITextAlignmentCenter;
lbNavTitle.text = NSLocalizedString(#"Milestone Details!",#"");
self.title = lbNavTitle // here self.title is your navigation bar title

Trouble adding custom button to navigation controller's toolbar

I have a tableview controller that has a navigation controller. I have added a toolbar to the bottom of the view and now I need to add a custom button. I have the following code (from viewDidLoad), which is producing strange results. When the view loads, I see the custom button for a brief second, then an empty toolbar. At the moment I am only trying to get one custom button to load.
self.navigationController.toolbar.barStyle = UIBarStyleBlackTranslucent;
[self.navigationController.toolbar sizeToFit];
CGFloat toolbarHeight = [self.navigationController.toolbar frame].size.height + 20;
[self.navigationController.toolbar setFrame:CGRectMake(CGRectGetMinX(self.view.bounds),
CGRectGetMaxY(self.view.bounds) - toolbarHeight,
CGRectGetWidth(self.view.bounds),
toolbarHeight)];
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithImage:[UIImage
imageNamed:#"stub_fav.png"]
style:UIBarButtonItemStylePlain
target:self action:#selector(addFavorite)];
[btn setTintColor:[UIColor whiteColor]];
[btn setImage:[UIImage imageNamed:#"stub_fav.png"]];
NSArray *toolbarItems = [[NSArray alloc] initWithObjects:btn, nil];
[self.navigationController.toolbar setItems:toolbarItems];
Any ideas? Thanks!
Im not sure what btnAddFave is for but it's not the subview of anything. I assume btw and btnAddFave should have the same background image, if so you spelled stub_fav.png wrong. shouldn't it be stub_fave.png

dismissing a UIPickerView once the user selected all rows

I have a UIPickerView with 2 components where the user can select a font and a font size.
I want to dismiss the view once the user has selected both.
Can I somehow do this using the didSelectRow:inComponent method?
Yes, if you record the component that is provided to the method then when all of your components have had a selection made you can dismiss.
Note that users may not like it very much and adding a toolbar above the picker with a done button might work better.
I would suggest creating a container view to hold the picker and the toolbar (subviews), then show / hide the container view with whatever animations you like (instead if where you currently show the picker view). The picker and the toolbar don't need to be linked in any way, just laid out nicely in the container.
I hope it will help you
UIActionSheet *action = [[UIActionSheet alloc] init];
action.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
UIPickerView *pickerFromage=[[UIPickerView alloc]initWithFrame:CGRectMake(0, 44, 0 , 0)];
UIToolbar *pickerAgeToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerAgeToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerAgeToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(PickerDoneClick)];
[doneBtn setTintColor:[UIColor blackColor]];
[barItems addObject:doneBtn];
[pickerAgeToolbar setItems:barItems animated:YES];
pickerFromage.delegate=self;
pickerFromage.dataSource=self;
pickerFromage.tag=value;
pickerFromage.showsSelectionIndicator=YES;
[action addSubview:pickerAgeToolbar];
[action addSubview:pickerFromage];
[action showInView:self.view];
[action setBounds:CGRectMake(0,0, 320, 464)];`
-(void)PickerDoneClick{
[action dismissWithClickedButtonIndex:0 animated:YES];
action=nil;
}
You can do it this way:
Take a UIView in xib and then add a UIPickerView and a toolbar with UIButton on it. Give the delegate to the UIPickerView and assign an action to the UIButton. Take an outlet of UIView and in your implementation file(ie. .m file). Hide the UIView and assign the frame to the bottom of the screen. Lets say you have named the view as myPickerView.
myPickerView.frame = CGRectMake(CGRectGetMinX(self.view.bounds),
CGRectGetMaxY(self.view.bounds),
CGRectGetWidth(myPickerView.frame),
CGRectGetHeight(myPickerView.frame));
myPickerView.hidden = YES;
After doing this you can animate the view from bottom to upwards by setting the frame again in animation block, and can also unhide it.
When you press done, again animate the view and set the frame as above.
Below is the code for animating
- (IBAction)showPicker:(UIButton *)sender {
self.myPickerView.hidden = NO;
[UIView animateWithDuration:0.5 animations:^{
[self.myPickerView setFrame:CGRectMake(0,
100,
CGRectGetWidth(self.myPickerView.frame),
CGRectGetHeight(self.myPickerView.frame))];
}];
}
action for done button:
-(IBAction)doneClicked:(id)sender {
self.myPickerView.hidden = NO;
[UIView animateWithDuration:0.5 animations:^{
[self.myPickerView setFrame:CGRectMake(CGRectGetMinX(self.view.bounds),
CGRectGetMaxY(self.view.bounds),
CGRectGetWidth(self.myPickerView.frame),
CGRectGetHeight(self.myPickerView.frame))];
}];
}
Hope this helps.

UIToolbar not showing UIBarButtonItem

I'm using a storyboard and I've a split view where the master is a UITableViewController. Like the iPad Mail app, I'd like to display a UIToolbar.
I wasn't able to add the toolbar via storyboard but I managed to add it programmatically. I'm also able to add a UILabel to the toolbar, but I can't find a way to add a refresh button or any kind of UIBarButtonItem.
Any idea?
- (void)viewDidLoad {
[super viewDidLoad];
[self.navigationController setToolbarHidden:NO];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50.0f, 0.0f, 80.0f, 40.0f)];
label.text = #"last updated...";
label.textAlignment = UITextAlignmentCenter;
label.font = [UIFont systemFontOfSize:13.0];
[self.navigationController.toolbar addSubview:label];
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:#"Item" style:UIBarButtonItemStylePlain target:self action:#selector(action:)];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:#"Item1" style:UIBarButtonItemStyleBordered target:self action:#selector(action:)];
NSArray *buttons = #[item1, item2, nil];
[self.navigationController.toolbar setItems:buttons animated:NO];
Found the answer thanks to the Apple iOS Forum!
When you use the toolbar of the navigation controller, you have to set the toolbar buttons on the active view controller's toolbarItems property, not on the actual navigation controller's toolbar itself.
From the UINavigationController docs:
Displaying a Toolbar
A navigation controller object manages an optional toolbar in its view
hierarchy. When displayed, this toolbar obtains its current set of
items from the toolbarItems property of the active view controller.
When the active view controller changes, the navigation controller
updates the toolbar items to match the new view controller, animating
the new items into position when appropriate.
For example:
[self setToolbarItems:buttons animated:NO];

Resources