Adjust UIPopoverController position after resize - cocoa-touch

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.

Related

iOS - hidden accessory becomes visible after orientation change

In my case I'm using a UITextField as the accessory that I don't need to show all the time. I've confirmed the change happens after the orientation notification events fire. I guess a hack would be to resize the accessory to zero height, but I'm reticent to do this.
Wondering if anyone has encountered this and found a solution?
Have entered a bug report and provided a sample project. For those with higher privileges, it is searchable on bugreport.apple.com as ID 16771757. I have also copied it to a Dropbox account accessible as https://www.dropbox.com/s/o28vo04ig3yhgz6/ID16771757.zip.
Thank you for reading.
iOS calls such methods for input accessory view instance:
[inputAccessoryView setAlpha:1]; when owner of accessory view becomes first responder (internal method call -[UIPeripheralHost(UIKitInternal) executeTransition:]);
[inputAccessoryView setHidden:NO]; when interface rotation finished (internal method call -[UIPeripheralHost finishRotationOfKeyboard:]);
That's why your input accessory view becomes visible after interface rotation event.
Solution depends on behaviour that you expect:
Let's imagine that input accessory view height = 44 ->
Now you hide input accessory view and set owner as first responder:
If you expect inputAccessoryView.frame.size.height equals 0 then solution for hiding input accessory view is set it to nil: inputAccessoryView = nil;
If you expect inputAccessoryView.frame.size.height equals 44 then solution for hiding input accessory view is override setHidden: method for it:
- (void)setHidden:(BOOL)hidden {
[super setHidden:self.customIsHiddenFlag];
}
where customIsHiddenFlag property that you need use for implementing logic of showing/hiding accessory view;
or override setAlpha: method:
- (void)setAlpha:(CGFloat)alpha {
[super setAlpha:(self.customIsHiddenFlag ? 0 : 1)];
}
These solutions work for iOS 7.
For iOS 6 you could use your base solution inputAccessoryView.hidden = YES and it works because iOS doesn't call setHidden:NO automatically when interface rotation is fired.
It seems that you are right and it's a bug cause of different behaviour on iOS6 and iOS7. If Apple wants to show input accessory view forcedly then they should call setHidden:NO not only after interface rotation but also when owner becomes first responder.
From Apple's documentation on inputAccessoryView:
… Subclasses that want to attach custom controls to either a system-supplied input view (such as the keyboard) or a custom input view (one you provide in the inputView property) should redeclare this property as readwrite and use it to manage their custom accessory view. [emphasis mine]
So the correct way to hide the accessory view would be redeclaring the property as readwrite:
#property (nonatomic, readwrite) UIView *inputAccessoryView;
removing the accessory view from superview and setting the property to nil when appropriate:
- (IBAction)hideAccessoryView:(UIButton *)sender
{
[self.inputAccessoryView removeFromSuperview];
self.inputAccessoryView = nil;
}
This is correct with regard to the docs but if you look at the view hierarchy, there's a UIPeripheralHostView (UIKit private class) that does not change its size. This most likely means that throwing out the accessory view will not be reflected by keyboard size — it'll stay the same. Keep this in mind if you plan to calculate any offsets to adjust to on-screen keyboard.
That said, the best way for you to move forward might be using a completely transparent view as the accessory view and have your custom view (UITextField in this case) as a subview. That way you will get both complete control over your custom view and consistent behaviour of your app on current and future versions of iOS.
Edit:
Here's a screenshot showing a slightly modified version of your bug report app with UIPeripheralHostView highlighted:
You can see how size of the view stays the same after the accessory view has been removed.
When you add an accessory view, you "pass" it to the system for layout. It is more than likely, when Apple performs layout on the keyboard view, it also layouts the accessory view and sets it to visible. Setting the accessory as hidden can also have other side effects, such as the keyboard height being incorrectly calculated, thus causing incorrect inset calculation.
From my experience, it is best to remove the accessory and add it again when necessary. Resizing the accessory view will cause other issues related to keyboard size. If you want to hide and show quickly, subclass the view that includes the accessory view, and implement internally the setting and removing of accessory view.

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.

Is there a function to know the useableSize of a view?

It must take into account:
statusBar (which can be 40 points if you have hot spot)
TabBar
NavigationBar.
Basically at viewDidLoad I see that my view has a size of 320*480.
I wonder where did iOS decide that as the screen size of my screen. I use UIStoryBoard.
So, on viewDidLoad, I intended to resize that.
I am not even sure if this is the right approach.
Note: the issue I am facing doesn't seem to happen if I do not use storyBoard.
At viewDidLoad, when I use XIB, the content of self.view is correct, namely 416, instead of 480, due to UInavigationController and UITabBar
Try overriding the UIViewController viewWillLayoutSubviews method. The view's frame will be set by then.
In the viewWillLayoutSubviews method, the view controller's main view is the size you need to know. It has been adjusted for status bars and nav bars and tools bars and tab bars. It also takes into account orientation. There is no single method where you can ask what the size will be. Besides, there is no need to ask such a question. Create all the subviews you want in viewDidLoad. But lay them out based on the view's size in viewWillLayoutSubviews.

Confusing behavior from UIPopoverController

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.

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.