ipad: popover is not appearing at appropriate place - objective-c

I have a situation here please help me out in this,
1) I have a Table with custom cells
2) Each cell has 2 search bars and 2 lables.
what I was attempting is suppose a user beginEditing a searchBar a popover should appear pointing that search bar.
I have implemented this but popover is not appearing over the desired searchBar moreover height of popover is also too long sometime
if (searchBar.tag==10) {
NSLog(#"Display date popover");
CGRect pickerFrame = CGRectMake(0,0,300,200);
UIViewController *tempDateViewController=[[UIViewController alloc] init];
UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
[datePicker addTarget:self action:#selector(pickerChanged:) forControlEvents:UIControlEventValueChanged];
[tempDateViewController.view addSubview:datePicker];
if(!currentPopover)
{
currentPopover=[[UIPopoverController alloc] initWithContentViewController:tempDateViewController];
}
else {
[currentPopover setContentViewController:tempDateViewController animated:YES];
}
tempDateViewController.contentSizeForViewInPopover=CGSizeMake(320, 300);
[datePicker release];
[tempDateViewController release];
[currentPopover presentPopoverFromRect:searchBar.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
Please help me to solve this one.
Thanx in advance.

The searchBar is inside a cell which is in a table view (which may be in some other view depending on your layout). The problem most likely is that self.view is not the direct parent of searchBar where you are calling this. So using the frame of the searchBar in self.view will give unexpected results.
Instead of using self.view and trying to figure out where the searchBar is relative to that, you can use the searchBar itself for the "inView" and for the "rect", use the searchBar's bounds:
[currentPopover presentPopoverFromRect:searchBar.bounds inView:searchBar
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
Next, to fix the height of the popover, try setting the popover's popoverContentSize instead of the view controller's contentSizeForViewInPopover:
//tempDateViewController.contentSizeForViewInPopover=CGSizeMake(320, 300);
currentPopover.popoverContentSize = CGSizeMake(320, 300);
Finally, a separate issue but, the minimum height for a datepicker should be 216, not 200:
CGRect pickerFrame = CGRectMake(0,0,300,216);

Related

Present UIImagePickerController inside a modal view or form sheet in iOS 7?

I'm not great at creating views in code yet so this is probably a simple question.
In my iPad app I have this image picker and I would like to present it in a 500x500 modal view centred on screen OR in a formsheet centre-screen:
_imagePicker = [[UIImagePickerController alloc] init];
_imagePicker.delegate = self;
_imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
_imagePicker.allowsEditing = NO;
_imagePicker.navigationBar.tintColor = [UIColor redColor];
_imagePicker.navigationBar.backgroundColor = [UIColor greenColor];
Now what do I do to both create the modal view or formsheet and present the image picker inside it?
(I've looked through many examples and Q&A and just can't find a simple answer. Thanks for your help!)
Form sheet is a modal presentation type. You change that by modifying the modalPresentationStyle property of a view controller before presentation.
_imagePicker.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:_imagePicker animated:YES completion:nil];
However, if you read the documentation of UIImagePickerController, you will see that Apple requires you to present the view controller inside a popover.
So:
if([_popoverController popoverVisible])
{
[_popoverController dismissPopoverAnimated:NO];
_popoverController = nil;
}
_popoverController = [[UIPopoverController alloc] initWithContentViewController:_imagePicker];
[_popoverController setDelegate:self];
[_popoverController presentPopoverFromBarButtonItem:_button permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

addSubview not showing up in NSView Objective C Cocoa

When I addSubview: nothing shows up. I set text and color to see it. Also if I manually add the view to the custom view in the UI bulder in xcode it shows up just fine with the text and color.
.m file
- (void)displayString:(NSString *)title {
NSRect frame = NSMakeRect(10, 10, 200, 17);
NSTextfield *newfield = [[NSTextField alloc] initWithFrame:frame];
[newfield setBezeled:NO];
[newfield setDrawsBackground:NO];
[newfield setEditable:NO];
[newfield setSelectable:NO];
[newfield setStringValue:title];
[newfield setTextColor:[NSColor blueColor]];
[test addSubview:newfield];
if([test.subviews containsObject:newfield]){
NSLog(#"view there"); // i get this message
}
if([newfield isHidden]){
NSLog(#"view hidden"); //i dont get this message
}
NSLog(#"view set");
}
test is a NSView (Custom view is what xcode calls it) that I have properly linked in.
So when I create the text field and add it to the NSView manually and then run that same code by adding text and color all works fine, this issue arrises when I try programmatically setting the view. Also I made sure it wasn't my creating of the view, as I have tried creating the view in the builder and not placing it in the NSView and then trying addSubview: but that also does not work. Let me know if you need more code.
DEVELOPMENT:
If the nsview (custom view) has an element already in it (manually added and can be anything) and I add the text field it works (I get both views in the nsview)? The subview is tested for and there, just cant see it.
You have to call initWithFrame: instead of just init
- (void)displayString:(NSString *)title {
NSRect frame = NSMakeRect(10, 10, 200, 200);
NSTextField *newfield = [[NSTextField alloc] initWithFrame:frame];
[newfield setStringValue:title];
[newfield setTextColor:[NSColor blueColor]];
[test addSubview:newfield];
NSLog(#"view set");
}
What type of view is test? Also you need to do a:
newfield.frame = CGRectMake(x,y,width,height)
in order to specify the look of the view
Turns out I set the view too early. I was under the impression that whatever you do to the view after its been set will be reflected on the view, but that seemed to be the issue. After altering the view to be exactly they way I want then set the view of the NSStatuditem.
so I get
[newfield setStringValue:title];
[newfield setTextColor:[NSColor blueColor]];
[test addSubview:newfield];
[statusItem setView:test];//this is the key, setting it after he changes.

UIPopovercontroller doesnt remain on its position when iPad rotated

I'm creating a popover like this.
-(void)showPop{
if (self.popoverController2 == nil) {
RootViewController *iap =
[[RootViewController alloc]initWithNibName:#"RootViewController" bundle:[NSBundle mainBundle]];
[iap setDelegate:self];
UIPopoverController *popover =
[[UIPopoverController alloc] initWithContentViewController:iap];
popover.delegate = self;
popover.popoverContentSize = CGSizeMake(320, 300);
self.popoverController2 = popover;
}
CGRect popoverRect = [self.view convertRect:[shopButton frame] fromView:[shopButton superview]];
[self.popoverController2 presentPopoverFromRect:shopButton.bounds inView:shopButton
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
ShopButton is a button from which the popover pops up. Now the problem is, when I rotate my device, The popover remains on the position it was drawn while the rest of the view adjusts according to the rotation. In simple words, when device is rotated with the popover drawn (popping up from UIButton), the popover doesn't move along with the UIButton. If I close the popover and again draw it from the button, It draws correctly above the button. The problem only comes when rotating with the popover popped up.
Please help ASAP.
Thank you :)
You can hide popover before orientation will change and show it again with new coords right after.

Subview of TableView removed - Tableview not visible

For the first time working with CorePlot (after a couple of hours trying to set it up :P )
on my view, i have a tableview. when a certain IBAction is called i want to show another view (a graph) instead of the tableview.
my approach was to add a subview with the same size to the tableview. it works fine to display the graph, but when i remove the graphs view from [table subviews] the tableview does not reappear.
note:
expenseTable: my tableView
hasSubView: (BOOL) that indicates if a graph is shown right now or not
code
-(IBAction)displayDayBalanceGraph:(id)sender{
if (hasSubView) {
[[expenseTable subviews] makeObjectsPerformSelector: #selector(removeFromSuperview)];
NSLog(#"%#",expenseTable.subviews);
}
else{
[self initializeMonthArray];
CPTGraphHostingView *host = [self buildGraphView];
[expenseTable addSubview:host];
CPTXYGraph *graph = [[CPTXYGraph alloc ]initWithFrame:host.frame];
host.hostedGraph = graph;
CPTScatterPlot *plot = [[CPTScatterPlot alloc]init ];
plot.dataSource = self;
[graph addPlot:plot];
[expenseTable reloadData];
hasSubView = !hasSubView;
}
}
-(CPTGraphHostingView *)buildGraphView{
CPTGraphHostingView *view = [[CPTGraphHostingView alloc]initWithFrame:CGRectMake(0, 0, 312, 260)];
[view setBackgroundColor:[self grayColor]];
return view;
}
1st Screenshot: TableView displayed
2nd Screenshot: GraphView displayed
sidenote: this is a sampleplot =)
3rd Screenshot: GraphView dismissed
has anyone an idea what i missed? (or messed ;) )
It's not generally a good idea to add views as subviews of UITableView.
Instead, you could either remove the table view and replace it with the Core Plot view:
[tableView removeFromSuperview];
[containerView addSubview:corePlotView];
Make sure you have a reference to the table view somewhere or it will be released.

Present formsheet modal ViewController using horizontal flip

I currently show a modal UIViewController in the formSheet style that was presented with the coverVertical animation. I am trying to present another modal UIViewController from it, using currentContext for the presentation style and flipHorizontal for the animation style. It does work, but there is a solid white white background behind the flip as it occurs. Any advice on how to successfully present another formsheet modal UIViewController using the flipHorizontal style from a pre-existing modal UIViewController in the formSheet style would be appreciated please! Thanks
Code:
// Loading of first modalVC
- (void)showModalVC {
Modal1VC *modal1VC = [[Modal1VC alloc] init];
modal1VC.modalPresentationStyle = UIModalPresentationFormSheet;
modal1VC.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:modal1VC animated:YES];
[modal1VC release];
modal1VC.view.superview.bounds = CGRectMake(0, 0, 400, 400);
}
// Loading of second modalVC in Modal1VC
- (void)buttonTapped:(id)sender {
UIViewController *modal2VC = [[UIViewController alloc] init];
modal2VC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
modal2VC.modalPresentationStyle = UIModalPresentationCurrentContext;
modal2VC.view.backgroundColor = [UIColor greenColor];
[self presentModalViewController:modal2VC animated:YES];
[modal2VC release];
modal2VC.view.superview.bounds = CGRectMake(0, 0, 400, 400);
}
UIViewController *presentingViewController = //allocate your VC
[modalViewController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
UIViewController *modalViewController = //your modal VC
[presentingViewController presentModalViewController:modalViewController animated:YES];
This is all the code you need ;)
It sounds like your transition is working, but the white background shown on the flip transition is the problem?
During the flip transition, the white background that is shown is actually the window. You can change the color that is shown by setting the backgroundColor property of window in the AppDelegate.
That would seem to be a limitation of presentModalViewController:animated:.
Instead of [self presentModalViewController:modal2VC animated:YES], you may be able to get the effect you want using UIView animations. Perhaps something like this:
[UIView transitionWithView:self.view.window
duration:0.3
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
[self presentModalViewController:modal2VC animated:NO];
} completion:NULL];