iOS: WEPopoverController in MKMapView - ios

Because i have more than one element to display at a given location in a mapview, i'd like to display a WEPopoverController in the MKMapView when a given MKPinAnnotationView has been selected.
Presenting the content, which is a tableview works fine so far. I've subclassed the MKPinAnnotationView and when the annotation view gets clicked, i call my custom presentation method.
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
[view openCustomPopoverInFrame:self.view.frame];
[mapView deselectAnnotation:view.annotation animated:NO];
if (_lastAnnotationView != view) {
[_lastAnnotationView closeCustomPopover];
_lastAnnotationView = view;
}
}
- (void) openCustomPopoverInFrame:(CGRect) frame {
CGRect fromFrame = self.frame; //Pin Annotation View Frame
WEPopoverContentViewController *contentViewController = [[WEPopoverContentViewController alloc] initWithStyle:UITableViewStylePlain];
contentViewController.delegate = self;
WEPopoverController *viewController = [[WEPopoverController alloc] initWithContentViewController:contentViewController];
//[viewController setPassthroughViews:[NSArray arrayWithObjects:contentViewController.view, contentViewController.tableView, nil]];
viewController.delegate = self;
//set the displayed content
....
_myPopoverController = [viewController retain];
[viewController presentPopoverFromRect:fromFrame inView:self.superview permittedArrowDirections:UIPopoverArrowDirectionUp | UIPopoverArrowDirectionDown animated:YES];
[contentViewController release];
[viewController release];
}
Also selecting buttons in the tableview cells work find and i get the expected response (the method gets called and the popovercontroller stays on top of the view hierarchy).
My problem is, when i click on the WEPopoverController Content (one of the table view cell, in the center, where no button is located), the tap event gets passed through to the map view and therefore hides the popover controller. What can i do, to prevent passed through tap events?
I tried several solutions e.g. set the passThroughViews and also manipulating the hitTest:withEvent: method in the WETouchableView and always return nil, but that also doesn't help me any further.
Best
Nic
EDIT
I've now debugged the hitTest:withEvent: method in the WETouchableView a little more in dept and it seems, that it always returns an instance of UITableViewCellContentView, which i think should be correct so far.
EDIT
So to be a little bit more clear, i want to display a popover controller and when it gets tapped it should neighter disappear like it is the usual annotation behavior nor the underlying view should get called. It should stay on top of the view and the tap event should get caught by the table view and call the specific method (tableView:didSelectRowAtIndexpath:).
I've looked at this solution aswell, but it seems not to work for me and the underlying view (wich is the map view) gets called.

Ok, what worked for me, was to add a UIButton with a transparent background color as the first subview of the table view cell. All Tap Events on the cell now get caught by the UIButton. Btw all swipe events get caught by the table view, so at least that works fine.
To be honest, this isn't a really nice solution, but it works for me. I'd really appreciate any further suggestions!
Best
Nic

Related

UITableView inside childviewcontroller not receiving all taps

I've got a UITableview that displays the search results of a UISearchController. They are inside of a Childviewcontroller.
I write the text on the textfield of the parentviewcontroller and it passes the text to the searchbar of the child view controller.
This all works fine.
But for some reason, when I am choosing a result in the child view controller's tableview, it is not very responsive.
After typing in the search text in the textfield (having the textfield as the firstResponder), most of the times I have to tap more than once to select a row.
(P.S. userInteraction is enabled, otherwise no touch would ever go through.)
Any idea why?
I have same problem, and my solution is:
- (void) displayContentController: (UIViewController*) content{
[content.view setFrame:recorderView.bounds];
UINavigationController *childNavController = [[UINavigationController alloc] initWithRootViewController:content];
childNavController.toolbarHidden = NO; // if you show toolbar
childNavController.view.frame = content.view.frame;
[self addChildViewController:childNavController];
[recorderView addSubview:childNavController.view];
[childNavController didMoveToParentViewController:self];
}
content is my subViewController
Is this for Swift or for Objective-C? Also are you placing a UI Tap Gesture Recognizer on the views that you wish to be touchable?

How to reset or restart root view of UIViewController to its original state?

I am new to iOS Development. I have a very simple UIViewController, with two button in the view. I am allowing users to edit UI components or drag and drop from one place to another. Now I want to implement reset functionality allowing users to reset UI to its original stage discarding all the changes.
As mentioned in several post tried setNeedsDisplay but looks like its not right way to achieve this. What are the my options ?
e.g. In Android world, you can finish current Activity (Equivalent of UIViewController) and relaunch self to achieve original state. Is it possible to do something like it in iOS.
- (IBAction)resetButtonClicked:(id)sender {
// Now I want discard all the changes on the screen
// On some button click or specific action from user.
// Tried calling setNeedsDisplay as pointed in few others answers but somehow
// setNeedsDisplay does not reload my ViewController.
// [self.view setNeedsDisplay];
}
I have done something similar. What I did was:
1) create local CGRect variables for each frame ui control i.e. CGRect originalFrame1;
2) get the original frame position for each ui control and store them on viewDidLoad i.e. originalFrame1 = _frame1.frame;
3) on the resetButtonClicked set it back to is original position _frame1.frame = originalFrame1;
I can think of two options:
1- Save the positions of your initial state, and when the user clicks the reset button, just put those buttons on their initial states.
2- OR (strange way of doing it), you can initialise the same controller you are in and just present it from itself, that should be somehow close to your android approach. You'll get the same controller on its initial state (If you didn't didn't dismiss the first one, you should)
Hope that helps :)
u can save the positions for all subviews(buttons,label,...) in the main view.
self.view.subview it will return array contain all elements u have it :)
and when u wanna rest your view just get the old position for each subview from the array.
NSArray *arrayOfView = [view subviews];
//also:
NSMutableArray *mutableArrayOfView = [[view subviews] mutableCopy];
for (UIImageView *imageView in imageViewArray) //uiimageview or whatever your subview //is you can check by using isKindofClass. `if ([view isKindOfClass:[UIImageView class]])`
{
//let's set frame for image view before adding to
[myimageView setFrame:imageview.frame];//or something like that
}
EVery app will have a keyWindow object for which you should assign some viewController to display.
window.rootViewController = [[UIViewController alloc] init];
Lets say your controller's class is CustomViewController
So, in your app delegate,didFinishLaunchingWithOptions, you will write
self.window.rootViewController = [[CustomViewController alloc] init];
And the function invoked when you click reset button be as below,
-(void)reset{
// reset the view with default
self.view.window.rootViewController = [[CustomViewController alloc] init];
}
If you follow above code, it won't have any memory leaks because ,when you set rootViewController again ,the old one will be lost and removed from memory.

Add clickable and fixed subview to UITableViewController?

I'd like to place an ADBannerView object onto my UITableView screen statically, what means that I want it to always stay above my toolbar (self.navigationController.toolbar), even when the user is scrolling the tableview. I've solved this by adding by ADBannerView as a subview to my toolbar and given it negative values for the frames origin:
[self setBannerViewSize];
[self.navigationController.toolbar addSubview:bannerView];
The only problem is: I can't click and open the iAd this way - I can see the banner but nothing happens when I tap on it.
Since I'm also using a refreshControl, the option to use a UIViewController instead of UITableViewController and add a tableView manually wouldn't work for me. Is there any other way I can get my ADBannerView statically showing in my table view controller AND still being tappable?
Thank you in advice!
Yay!! After all I succeeded in solving this (really annoying) problem by myself (and a lot of reading around)!
First, I found this really world-changing post. Basically this post handles with the topic that a UITableViewController uses self.view for its tableView property, so overriding the tableView property (or synthesizing it manually) plus giving self.view a new view (from application) and adding tableView as its subview would make it possible to reach the real superview of tableView.
But this still didn't solve my problem, although I was sure it would, because it all made sense. My bannerView appeared in the right place (and was fixed) but it still didn't do anything when clicked. But there was a second minor thing I didn't know about:
As I read in this post the superview of a subview doesn't only have to be userInteractionEnabled but also have a non-transparent backgroundColor. Because my superviews background color was set to [UIColor clearColor] it all didn't work - but setting its backGroundColor to e.g. blackColor solved the whole problem: the bannerView got finally tappable! :)
So, my code is now looking like this:
#synthesize tableView;
- (void)viewDidLoad
{
[super viewDidLoad];
if (!tableView && [self.view isKindOfClass:[UITableView class]]) {
tableView = (UITableView *)self.view;
}
self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
self.tableView.frame = self.view.bounds;
[self.view addSubview:self.tableView];
[self resizeTableToFitBanner];
self.view.backgroundColor = [UIColor whiteColor];
[self.view addSubview:bannerView];
// some other code
}
BannerViewController in Apple's iAdSuite sample code solves this problem very elegantly:
https://developer.apple.com/library/ios/samplecode/iAdSuite/Introduction/Intro.html
I think you should use a container view, and set things up in IB. You can add a tool bar and ADBannerView to the bottom of the view of your navigation controller's root view controller. Fill the rest of the space with a container view - this will give you an embedded view controller automatically. You should delete this one and then drag in a tableViewController and control drag from the container view to the tableViewController to hook up the embed segue.

Push view controller after setting view

Suppose i have a uiview in 1 screen and i want to view the same view in fullscreen mode on click of a button.
On click of a button the following function is called.
-(IBAction)fullScreen
{
FullScreenViewController *mv = [[FullScreenViewController alloc] init];
mv.fullview = minimizedView;
//minimizedView is a UIView already created with a specified frame
// fullview is a UIView decalred in FullScreenViewController
[[self navigationController] pushViewController:mv animated:YES];
}
In the FullScreenViewController.m the viewDidLoad function is as follows :
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view addSubview:fullview];
}
On click of the fullscreen button, the view appears but on click of back button, the minimized view in the previous page dissapears.
Is it wrong to do this?
You dont need to use two views to implement this. In your code, your are navigating from one view to another view. You dont need to do that. Your minimizeView itself having a property "setFrame:" to increase and decrease its frame to resize of that subview in your current view itself. Learn how to resize the UIView.

How do I display a UIPopoverView as a annotation to the map view? (iPad)

On the maps app in the IPad when you tap a pin you get a normal annotation with an "i" instead of a disclosure indicator. A further tap on the "i" reveals a popover view controller like this.
Is there a way to easily achieve this?
First add an annotation to the map and in the viewForAnnotation method, set the rightCalloutAccessoryView to a button of type, say, UIButtonTypeDetailDisclosure (I don't think the blue info button is available by default).
Pressing the button will call the calloutAccessoryControlTapped delegate method. In this method, deselect the annotation and show your popover. For example:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
[mapView deselectAnnotation:view.annotation animated:YES];
YourContentViewController *ycvc = [[YourContentViewController alloc] init...
UIPopoverController *poc = [[UIPopoverController alloc] initWithContentViewController:ycvc];
[ycvc release];
//hold ref to popover in an ivar
self.annotationPopoverController = poc;
//size as needed
poc.popoverContentSize = CGSizeMake(320, 400);
//show the popover next to the annotation view (pin)
[poc presentPopoverFromRect:view.bounds inView:view
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[poc release];
}
YourContentViewController is a subclass of UIViewController which you can code like any other view controller. The Maps app looks like it has UITableView in the content.
It appears that to have a better position for the popover you must present it from this rect:
CGPoint lc_point = [mapView convertCoordinate:view.annotation.coordinate toPointToView:mapView];
CGRect lc_frame = CGRectMake(lc_point.x,lc_point.y-view.frame.size.height,0,0);
You can use a library like Gordon Hughes' Animated Callout. Unfortunately it's still not working perfectly on iOS 6 (callouts show up strangely).
Here's iOS 5:
My requirement was similar to this issue, but also needed to allow users to interact with the map (panning and zooming) while the callout is displayed.
For this I've created this github project: https://github.com/crarau/mapkit-custom-callout

Resources