Confusing behavior from UIPopoverController - objective-c

I'm trying to set up a popover to appear that displays a UIDatePicker when I press a button, however I'm getting some very confusing behavior. I created a view controller that housed nothing but the UIDatePicker, wired one up in the class i needed it in, and added it to a new UIPopoverController like this:
self.timePickerPopoverController = [[UIPopoverController alloc] initWithContentViewController:self.timePickerViewController];
then I present it like so:
[timePickerPopoverController presentPopoverFromRect:prepTimeButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
(prepTimeButton being the button that was pressed). However, I just get the following result:
Instead of it displaying next to the button that was pressed and at the target size (it's way too tall right now; should only be the size of the date picker). I also tried giving it a custom view of the proper location and size in which to display, but that didn't help much (just shifted the popover to the right half of the screen). What am I doing wrong and how do I fix it?

Is self.view the direct superview of prepTimeButton? Perhaps prepTimeButton is nested in a subview, in that case you'd need to use that as the inView: parameter (or convert the coordinates).
Did you set the contentSizeForViewInPopover property of your view controller?

Make sure to set both contentSizeForViewInPopover on the internal view controller and popoverContentSize on the UIPopoverController itself.

Related

Button is disabled when using UITableViewCell instance as if it was a UIView instance

There is a custom view that I extend from UITableViewCell.
In CustomTableViewCell, I implement init method so that it loads a corresponding .xib which contains button.
However, I want to reuse it as if it's a normal UIView instance. For instance, I want add it as a child of my view controller
// in view controller
CustomTableViewCell* customTableViewCell = [[CustomTableViewCell alloc] init];
[self.view addSubview:customTableViewCell];
The problem is, the custom table view cell can be added, but the button that it holds can not be pressed. Meanwhile, the button of the custom table view cell that is populated by table view is working fine.
https://www.evernote.com/shard/s27/sh/59f7b828-f65a-41dc-9156-40a985fceac8/9edb04b8e5c821ad7a46eb714cc776dc
In the screenshot above, the button with the yellow highlight can not be pressed, while the blue can.
Anyone has the idea why it's not working?
It's a guess, but you create the cell with a CGRectZero frame. It doesn't clip it's subviews so they can still be seen, but it's subviews won't get touches outside of the superview's bounds (which are zero). Try alloc initWithFrame:CGRect(/* some reasonable values */).
Once you get that working, you can try with some layout constraints.
May be you need pass fileowner in 'init' method, if you have assigned button's actions to fileowner in xib-file.

Always visible UIView

I want to place a UIView that will inform the user what is the status of the app. I want that view to be visible even if the user switches views, same thing as the UINavigationBar is always visible, but I don't want to use that bar, I would like to add another view that will show a message.
How can this be done? I can't add the view to the current view, because it will disappear, if the user changes views.
It should be added to the window? But how? I would then have to resize the views so that my new view can fit, but how?
Thanks.
Create a container view controller and set it as the rootViewController of the apps window.
Inside this container you have your status view, and you also resize the windows real rootViewController to take up the remaining space as a subview. If you are using a standard container view conrtoller (tab, navigation etc) as the root then you can use standard navigation methods and the status view will always be visible
There would be problems if you wanted to present modal views though, since these would go over the top of the status view
In your appDelegate.m add your view as subView of window.
UIView *mainBg = [[UIView alloc] init];
mainBg.frame = newframe;
[self.window addSubview:mainBg];
If you have a UINavigationController you can add your view to its view.
[self.navigationController.view addSubView:yourView];
Presenting modal views would cover the view, as stated in the other answer.

presentPopoverFromRect is displaying a popover sideways on rotation

-I have a UIView.
-This UIView has a UIButton that when clicked makes a UIAlertView appear.
-Within this UIAlertView I have another UIButton that when clicked calls buttonClicked:
-Within this buttonClicked: method, I call presentPopoverFromRect with a custom view inside. (hourKeyboard is the custom view)
-(void)buttonClicked:(id)sender
{
if(self.hourKeyboard==nil)
{
self.hourKeyboard = [[HourKeyboardViewController alloc] init];
self.hourKeyboardPopover = [[UIPopoverController alloc] initWithContentViewController:self.hourKeyboard];
}
[self.hourKeyboardPopover presentPopoverFromRect:[sender bounds] inView:sender permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
}
In normal portrait mode, this works great. The popover spawns just to the right of the button, with the arrow correctly pointing left to the button.
There's 2 problems that arrises:
1) While this popover is visible, when you rotate the screen the popover rotates slightly incorrectly (it doesn't reposition it's own x and y position)
2) If the popover is not being shown. If you rotate the screen, then call "buttonClicked", the popover will appear, however, its being shown sideways above the button with the arrow pointing "down" towards the button (technically left in relation to the sideways popover view). If you dismiss it, rotate the screen, then call "buttonClicked", the popover now appears upside down with the button pointing "right" to the button (again, technically left in relation to the sideways popover view)! Repeat to make it sideways again, then right-side up again.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
My thinking:
1) I believe I can just reposition the x and y, however, I've also read that you should dismiss the popover and present it over again on a rotation. I'll see if I can get the first one working, however I'm more concerned about the second problem.
2) I have no idea how to fix this rotation issue. It seems that when you rotate to landscape without the popover being visible. And then you call presentPopoverFromRect, the popover is created with the iPad thinking it's still in portrait view by mistake. That's the behavior it's giving, however, I'm not sure how to make the iPad not make this mistake.
-=-=-=-=-=-=-=-=-=
Thanks again for any help you can provide!
-=-=-
Slight Update:
1) It was easy to just dismiss the popover from the main view on rotation. And this seems to be the general way everyone deals with this issue.
2) Trying out various things such as changing the frame, using CGAffineTransformMakeRotation, and others...but no luck thus far
-=-=-
Another Update:
2) After a lot of testing, it seems to be a direct issue with UIAlertView. If I place the view within UIAlertView (currently doing), the AlertView doesn't tell the popover that the screen is rotated...thus creating the issue
It looks like the only way to fix this is to drop the UIAlertView completely. Instead of showing the UIAlertView, I'll disable the various background views manually (like Alert View was doing) and then show a custom UIView that looks darn similar to the AlertView. From there, I should be able to show the popover without any issues. I'll let yea know how it turns out.
-=-=-=-=-=-=-=-
Final Solution:
I ended up just creating my own view, and having that view imitate a UIAlertView. Then when I spawned the popover, I placed it in the root view controller. Worked much MUCH better, but required more work since I had to manually create my own View instead of the premade UIAlertView. Either way, apparently UIAlertView fails at telling a UIPopoverover subview what rotation it is in.
dismiss the popover in willRotateToInterfaceOrientation and show it again in didRotateFromInterfaceOrientation.
It works with no problems.
EDIT:
Sorry, I misunderstood your second problem.
If some part of your view hierarchy is displayed with bad orientation, one of your controllers is probably missing shouldAutorotateToInterfaceOrientation method.

Width of Modal Sheet on iPad

I am presenting an instance of UINavigationController modally with the modalPresentationStyle set to UIModalPresentationFormSheet. Inside the UINavigationController is an UITableViewController. For a delete button in the table view, I have to know the width of the sheet. Is there any way to access this?
I tried [self.view bounds], however this returns the with of the screen. I think that's because the view is not visible at the time a call this, however I don't know where else I should call it.
You can directly measure and store a magic value for the form sheet width (not pretty, I know, but it works) the value that I have been using is 540.
If you really want to get it programatically. You'll have to make sure that the view has been resized and prepped for presentation before taking the bounds value from the view. I believe this happens somewhere between viewWillAppear and viewDidAppear, although I can't remember exactly.
If all you want to do is ensure a button is aligned to the right, try using the autoresizingMask property of the button. I believe you'll only need to use UIViewAutoresizingFlexibleLeftMargin.
You can do "last minute" view setup and adjustments in viewDidLoad -- all the nib outlets have been initialized by that time.

Adjust UIPopoverController position after resize

I have a UIPopoverController containing a UITableView. The popover is resized in its view controller's -viewDidAppear function to fit the contents of the table. While the popover resizes properly, its arrow is usually no longer pointing at the original CGRect. Is there a way to force the popover to reposition itself after a resize so that its arrow is pointing at its intended target?
EDIT: I can't set the size of the popover in -viewDidLoad since the table view does not load its data until -viewDidAppear is called, and as a result I do not know what size the popover should be until then. In addition, I resize the popover when one of the table view cells is clicked to display another view and this also results in the arrow no longer pointing at its intended target.
I think this may be the wrong way to go about it, since you're having to re-do the built-in behaviour that positions the arrow to begin with.
I don't resize popover content in viewDidAppear. I set the contentSizeForViewInPopover property in the view controller's viewDidLoad method, e.g.:
- (void)viewDidLoad
{
[super viewDidLoad];
self.contentSizeForViewInPopover = CGSizeMake(320, 155); // sized for a 3-row UITableView
}
(Quick warning: if you're developing a universal app, this code will cause a run-time crash on devices running 3.1.x and below.)
You can also set the content size for the popover controller before you present it, which should take care of your problem. Check out the popoverContentSize property.
According to this answer, you can call presentPopoverFromRect:inView: on the popover again and it will reposition the arrow. I haven't tested this myself.