iOS8: Not show UIPopoverController but iOS7 is OK - ios

Apple seems to change the attitude of UIPopoverController since iOS8.
Please look at the code below. I want to show UIPopoverController after addSubview. iOS6 and iOS7 are no problem but it does not display on iOS8.
Could you suggest any possible reasons? Any help will be appreciated it.
- (IBAction)tapButton:(id)sender {
SampleView *sampleView = [[SampleView alloc] initWithFrame:CGRectMake(0.0f, 0.0f,
[UIScreen mainScreen].bounds.size.width,
[UIScreen mainScreen].bounds.size.height)];
sampleView.backgroundColor = [UIColor blueColor];
sampleView.alpha = 0.5;
sampleView.label = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 2,
[UIScreen mainScreen].bounds.size.height / 2,
200.0f, 20.0f)];
sampleView.label.text = #"SAMPLE TEXT";
sampleView.label.textColor = [UIColor redColor];
[sampleView addSubview:sampleView.label];
[self.view.window addSubview:sampleView];
if (!self.popover) {
UIViewController *vc = [[UIViewController alloc] init];
vc.view.backgroundColor = [UIColor redColor];
vc.preferredContentSize = CGSizeMake(200, 300);
self.popover = [[UIPopoverController alloc] initWithContentViewController:vc];
}
// On iOS7 is OK but not show on iOS8
[self.popover presentPopoverFromRect:CGRectMake(200, 100, 0, 0)
inView:sampleView.label
permittedArrowDirections:UIPopoverArrowDirectionUp
animated:YES];
}
Update1:
I followed by this question's answer.
in iOS8: UIPopoverController presentPopoverFromRect not work for keyWindow any more
This is updated code.
[self.popover presentPopoverFromRect:CGRectMake(200, 100, 0, 0)
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionUp
animated:YES];
As far as I checked, keyWindos and self.view.window seems to be same.
NSLog(#"%#", [UIApplication sharedApplication].keyWindow);
NSLog(#"%#", self.view.window);
However, this is not complete solution for my case. I want to control views on the added subview.
Update2:
I guess that I need to use UIPopoverPresentationController on iOS8.
- (IBAction)tapButton:(id)sender {
// View
SampleView *sampleView = [[SampleView alloc] initWithFrame:CGRectMake(0.0f, 0.0f,
[UIScreen mainScreen].bounds.size.width,
[UIScreen mainScreen].bounds.size.height)];
sampleView.backgroundColor = [UIColor blueColor];
sampleView.alpha = 0.5;
// Label
sampleView.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 20.0f)];
sampleView.label.text = #"SAMPLE TEXT";
sampleView.label.textColor = [UIColor redColor];
[sampleView addSubview:sampleView.label];
// View Controller
UIViewController *viewController = [[UIViewController alloc] init];
viewController.modalPresentationStyle = UIModalPresentationPopover;
viewController.view = sampleView;
// UIPopoverPresentationController
UIPopoverPresentationController *presentationController = viewController.popoverPresentationController;
[presentationController setDelegate:self];
presentationController.permittedArrowDirections = 0;
presentationController.sourceView = [[UIApplication sharedApplication] keyWindow];
presentationController.sourceRect = [[UIApplication sharedApplication] keyWindow].bounds;
[viewController setPreferredContentSize:CGSizeMake(320, 480)];
[self presentViewController:viewController animated:YES completion:nil];
if (!self.popover) {
UIViewController *vc = [[UIViewController alloc] init];
vc.view.backgroundColor = [UIColor redColor];
vc.preferredContentSize = CGSizeMake(200, 300);
vc.modalPresentationStyle = UIModalPresentationPopover;
self.popover = [[UIPopoverController alloc] initWithContentViewController:vc];
self.popover.delegate = self;
}
[self.popover presentPopoverFromRect:CGRectMake(300, 300, 0, 0)
inView:viewController.view
permittedArrowDirections:UIPopoverArrowDirectionUp
animated:YES];
}
Update3 I used dispatch_async but not worked. Some say that dispatch_async is effective in this case. dispatch_async is very simple solution. I prefer to use it. If you can fix my problem by using dispatch_async, I would be grateful you could attache the code.
- (IBAction)tapButton:(id)sender {
SampleView *sampleView = [[SampleView alloc] initWithFrame:CGRectMake(0.0f, 0.0f,
[UIScreen mainScreen].bounds.size.width,
[UIScreen mainScreen].bounds.size.height)];
sampleView.backgroundColor = [UIColor blueColor];
sampleView.alpha = 0.5;
sampleView.label = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 2,
[UIScreen mainScreen].bounds.size.height / 2,
200.0f, 20.0f)];
sampleView.label.text = #"SAMPLE TEXT";
sampleView.label.textColor = [UIColor redColor];
[sampleView addSubview:sampleView.label];
[self.view.window addSubview:sampleView];
if (!self.popover) {
UIViewController *vc = [[UIViewController alloc] init];
vc.view.backgroundColor = [UIColor redColor];
vc.preferredContentSize = CGSizeMake(200, 300);
self.popover = [[UIPopoverController alloc] initWithContentViewController:vc];
}
// I tried this but not worked.
dispatch_async(dispatch_get_main_queue(), ^{
[self.popover presentPopoverFromRect:CGRectMake(200, 100, 0, 0)
inView:sampleView.label
permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
});
}

Try and do the following for iOS 8
dispatch_async( dispatch_get_main_queue(), ^{
[self.popover presentPopoverFromRect:CGRectMake(200, 100, 0, 0) inView:sampleView.label permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
});
EDIT
This is my code, it works With or without dispatch_async.
self.popover = [[UIPopoverController alloc] initWithContentViewController:[self.storyboard instantiateViewControllerWithIdentifier:#"second"]];
dispatch_async(dispatch_get_main_queue(), ^{
[self.popover presentPopoverFromRect:self.button.frame inView:self.button permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
});

Related

UIPopoverController is not presenting controller in assigned position

I am trying to present a popover on clicking a button. When I tap on button, it shows popover on top of the screen and background is completely black. I have set the position and gave hard coded origin to popover but still it is going on top.
Here is the snapshot:
Here is the code I am using:
UIPopoverController *popOverController = [[UIPopoverController alloc]
initWithContentViewController:myViewController];
CGRect frame = CGRectMake(20, 30, 100, 50);
popOverController.popoverLayoutMargins = UIEdgeInsetsMake(0, frame.origin.x, 0, 0);
popOverController.delegate = self;
[popOverController setBackgroundColor:[UIColor clearColor]];
[popOverController presentPopoverFromRect:frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
EDIT
I have changed my code and tried this:
UIView *callOut = [[UIView alloc] initWithFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, 100, 50)];
callOut.backgroundColor = [UIColor whiteColor];
UILabel *callOutLable = [[UILabel alloc] initWithFrame:CGRectMake(callOut.frame.origin.x-5, callOut.frame.origin.y-5, 80, 40)];
[callOutLable setText:#"Callout"];
[callOut addSubview:callOutLable];
UIViewController *controller = [[UIViewController alloc] init];
controller.view = callOut;
UIPopoverController *popOverController = [[UIPopoverController alloc]
initWithContentViewController:controller];
popOverController.delegate = self;
CGRect frame = callOut.frame;
frame.size.height = 100;
frame.size.width = 100;
popOverController.popoverContentSize = CGSizeMake(100.0, 50.0);
[popOverController setBackgroundColor:[UIColor blueColor]];
[popOverController presentPopoverFromRect:frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
As I am creating a custom view which is really small. Now it shows the label on assigned position but still the background view is white.
Now what I am missing here? Any clue?
TestViewController *testViewController = [[TestViewController alloc] initWithNibName:#"TestViewController" bundle:nil];
testViewController.delegate = self;
self.userDataPopover = [[UIPopoverController alloc] initWithContentViewController:testViewController];
self.userDataPopover.popoverContentSize = CGSizeMake(320.0, 400.0);
[self.userDataPopover presentPopoverFromRect:[(UIButton *)sender frame]
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
See this example, you should give content size to pop over as above. But you declared directly as CGRect.

Add existing view controllers in scrollview

I have two view controllers and I wanted to add this as subview of my scrollview, but, when I add these view controllers the second controller is only displayed.
This is my code.
- (void)viewDidLoad
{
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[self.view addSubview:scrollView];
[scrollView setBounces:NO];
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(320*2, 460);
controllers = [[NSMutableArray alloc] initWithCapacity:0];
First *first = [self.storyboard instantiateViewControllerWithIdentifier:#"First"];
[scrollView addSubview:first.view];
[controllers addObject:first];
Second *second = [self.storyboard instantiateViewControllerWithIdentifier:#"Second"];
[scrollView addSubview:second.view];
[controllers addObject:second];
}
Thanks in advance.
Just give you an example, you can adjust their frame according to your requirement.
- (void)viewDidLoad
{
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[self.view addSubview:scrollView];
[scrollView setBounces:NO];
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(320*2, 460);
controllers = [[NSMutableArray alloc] initWithCapacity:0];
First *first = [self.storyboard instantiateViewControllerWithIdentifier:#"First"];
[scrollView addSubview:first.view];
first.view.frame = CGRectMake(0.0, 0.0, 320.0, 460.0) ;
[controllers addObject:first];
Second *second = [self.storyboard instantiateViewControllerWithIdentifier:#"Second"];
[scrollView addSubview:second.view];
second.view.frame = CGRectMake(320.0, 0.0, 320.0, 460.0) ;
[controllers addObject:second];
}

issue in setting image of navigation bar while presentModalViewController

this is how i set image in my first class in viewDidLoad which is tableView
if ([self.navigationController.navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)]) {
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"header.png"] forBarMetrics:UIBarMetricsDefault];
}
now this how i go to detail view
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:dvController animated:YES];
in detail view's bar the image which i have set for navigation bar comes automatically there and everything works perfect
now my problem is that pushViewController is working perfectly images are getting displayed
but i get default image in presentModelViewController this is how i use it
- (void) modelappear
{
if (self.testModalViewController == nil)
{
TestModalViewController *temp = [[TestModalViewController alloc] initWithNibName:#"TestModalViewController" bundle:[NSBundle mainBundle]];
self.testModalViewController = temp;
[temp release];
}
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self.testModalViewController];
[self presentModalViewController:nav animated:YES];
}
Note i just set buttons in inner hierarchy views
Can you please tell me what am i doing wrong? please give explanation with answer thank you so much
try like this ,
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:[NSBundle mainBundle]];
dvController.topViewController.navigationController.navigationBar.tintColor = [UIColor colorWithPatternImage:[UIIMage imageNamed:#"name.png"]];
[self presentModalViewController:nav animated:YES];
UIImage *image = [UIImage imageNamed:#"header.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.navigationController.navigationBar addSubview:imageView];
UIImage *image = [UIImage imageNamed:#"header.png"];
UIImageView *imageViewe = [[UIImageView alloc] initWithImage:image];
UILabel *tmpTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 3, 150, 40)];
[tmpTitleLabel setFont:[UIFont boldSystemFontOfSize:18]];
tmpTitleLabel.text = #"Add Manually";
tmpTitleLabel.backgroundColor = [UIColor clearColor];
tmpTitleLabel.textColor = [UIColor whiteColor];
CGRect applicationFrame = CGRectMake(0, 0, 300, 40);
UIView * newView = [[[UIView alloc] initWithFrame:applicationFrame] autorelease];
[newView addSubview:imageViewe];
[newView addSubview:tmpTitleLabel];
[self.navigationController.navigationBar addSubview:newView];

iOS TabBarController background color

I have a tabbarcontroller that is pushed onto a navigationController. I tried to change the background color of the tab bar however it does not work:
UIViewController *viewController1, *viewController2;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
viewController1 = [[Tab1_iPhone alloc] initWithNibName:#"tab1_iPhone" bundle:nil];
viewController2 = [[Tab2_iPhone alloc] initWithNibName:#"tab2_iPhone" bundle:nil];
}
self.tabBarController = [[UITabBarController alloc] init];
CGRect frame = CGRectMake(0.0, 0.0, 480, 48);
UIView *v = [[UIView alloc] initWithFrame:frame];
[v setBackgroundColor:[UIColor blueColor]]; //003366
[v setAlpha:1.0];
[[self.tabBarController tabBar] insertSubview:v atIndex:0];
self.tabBarController.viewControllers = [NSArray arrayWithObjects: viewController1, viewController2, nil];
[self.navigationController setNavigationBarHidden:YES];
[self.navigationController pushViewController:self.tabBarController animated:YES];
[self.window makeKeyAndVisible];
This is the code I saw on a similar post that changes the bg color:
CGRect frame = CGRectMake(0.0, 0.0, 480, 48);
UIView *v = [[UIView alloc] initWithFrame:frame];
[v setBackgroundColor:[UIColor blueColor]]; //003366
[v setAlpha:1.0];
[[self.tabBarController tabBar] insertSubview:v atIndex:0];
Am I doing something wrong?
Thanks
The below code helps you to add custom colors with RGB values to ur tabBar.
self.tabBarController.tabBar.tintColor = [[UIColor alloc] initWithRed:0.00
green:0.62
blue:0.93
alpha:1.0];

Trying to construct a tableview with a navigation bar at top

Here's the code I used. What am I missing?
- (void)loadView
{
CGSize screen_size = [[UIScreen mainScreen] bounds].size;
CGFloat navBarHeight = 40;
UINavigationBar *nav = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, screen_size.width, navBarHeight)];
UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, navBarHeight, screen_size.width, screen_size.height - navBarHeight) style:UITableViewStylePlain];
table.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
table.delegate = self;
table.dataSource = self;
table.editing = YES;
[table reloadData];
[self.view addSubview:nav];
[self.view addSubview:table];
[nav release];
[table release];
}
Instead of a nav bar with a table underneath, I get a black screen under the status bar.
You need to create a containing view in your loadView method and set it as the view on your view controller:
- (void)loadView {
CGSize screen_size = [[UIScreen mainScreen] bounds].size;
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0,0,screen_size.width,screen_size.height)];
self.view = myView;
CGFloat navBarHeight = 40;
UINavigationBar *nav = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, screen_size.width, navBarHeight)];
UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, navBarHeight, screen_size.width, screen_size.height - navBarHeight) style:UITableViewStylePlain];
table.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
table.delegate = self;
table.dataSource = self;
table.editing = YES;
[table reloadData];
[self.view addSubview:nav];
[self.view addSubview:table];
[nav release];
[table release];
[myView release];
}
Or alternately, if you have a nib file associated with your view controller then you should be using the viewDidLoad method instead of loadView.

Resources