issue in setting image of navigation bar while presentModalViewController - ios

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];

Related

UITabBar Wierd Height Difference

I want the default height as seen in the Storyboard image when adding the bar programmatically. Any idea how to get it? Here is the code I use to add a UITabBarController to my app.
- (void)setCustomTabBar
{
//TODO: Fix TabBar height
UITabBarController *tabBarController = [[UITabBarController alloc] init];
TimelineVC *tvc = [[TimelineVC alloc] initWithNibName:#"TimelineVC" bundle:nil];
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:tvc];
TimelineVC *tvc2 = [[TimelineVC alloc] initWithNibName:#"TimelineVC" bundle:nil];
UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:tvc2];
tvc2.showFriends = true;
NotificationVC *nvc = [[NotificationVC alloc] initWithNibName:#"NotificationVC" bundle:nil];
nav3 = [[UINavigationController alloc] initWithRootViewController:nvc];
ProfileVC *pvc = [[ProfileVC alloc] initWithNibName:#"ProfileVC" bundle:nil];
UINavigationController *nav4 = [[UINavigationController alloc] initWithRootViewController:pvc];
[[UITabBar appearance] setTintColor:[UIColor whiteColor]];
[[UITabBar appearance] setShadowImage:nil];
[tabBarController setViewControllers:#[nav1, nav2, nav3, nav4]];
tabBarController.selectedIndex = 0;
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat tabBarWidth = screenRect.size.width/4;
[[UITabBar appearance] setBackgroundImage:[AppDelegate imageFromColor:[UIColor darkGrayColor] forSize:CGSizeMake(screenRect.size.width, 49) withCornerRadius:0]];
[[UITabBar appearance] setSelectionIndicatorImage:[AppDelegate imageFromColor:[UIColor colorWithRed:26/255.0 green:163/255.0 blue:133/255.0 alpha:1] forSize:CGSizeMake(tabBarWidth, 49) withCornerRadius:0]];
tabBarController.tabBar.translucent = NO;
UIImage *normalImage, *selectedImages;
normalImage =[UIImage imageNamed:#"TimeLineIcon"];
selectedImages = [UIImage imageNamed:#"TimeLineIcon"];
UIImage *normalImage2, *selectedImages2;
normalImage2 =[UIImage imageNamed:#"FriendsIcon"];
selectedImages2 = [UIImage imageNamed:#"FriendsIcon"];
UIImage *normalImage3, *selectedImages3;
normalImage3 =[UIImage imageNamed:#"NotificationIcon"];
selectedImages3 = [UIImage imageNamed:#"NotificationIcon"];
UIImage *normalImage4, *selectedImages4;
normalImage4 =[UIImage imageNamed:#"ProfileIcon"];
selectedImages4 = [UIImage imageNamed:#"ProfileIcon"];
//TODO: FOR LOOP
nav1.tabBarItem.selectedImage = [normalImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
nav1.tabBarItem.image = [selectedImages imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
nav1.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
nav2.tabBarItem.selectedImage = [normalImage2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
nav2.tabBarItem.image = [selectedImages2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
nav2.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
nav3.tabBarItem.selectedImage = [normalImage3 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
nav3.tabBarItem.image = [selectedImages3 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
nav3.tabBarItem.badgeValue = #"1";
nav3.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
nav4.tabBarItem.selectedImage = [normalImage4 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
nav4.tabBarItem.image = [selectedImages4 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
nav4.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
self.window.rootViewController = tabBarController;
[self.window addSubview:tabBarController.view];
[self enableSupportKit];
}
[self.tabBar setFrame:CGRectMake(self.tabBar.frame.origin.x, self.tabBar.frame.origin.y, self.tabBar.frame.size.width, self.tabBar.frame.size.height)];
This helped me.
Hope it helps.
checkout your launchImage.for example,it's correct in your simulator,but if you use your iphone(i guess is iphone6). remove iphone6 launchimage,then you will find the question.

iOS8: Not show UIPopoverController but iOS7 is OK

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];
});

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.

placing a Tabbar controller on top of viewcontroller

How do i place the TABBAR control on top of view control.by default it is at bottom how do i put it up..?
Used following code to do:
UIImage* tabBarBackground = [UIImage imageNamed:#"footer-bg.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
[[UITabBar appearance]setSelectionIndicatorImage:[UIImage imageNamed:#"footer-hover-bg.png"]];
if(IS_IOS_7)
{
[[appDelegate.tabBarController tabBar] setSelectionIndicatorImage:[UIImage imageNamed:#"footer-hover-bg.png"]];
[[UITabBar appearance] setTintColor:[UIColor blackColor]];
}
appDelegate.tabBarController.tabBar.frame = CGRectMake(0, 20, 320, 50);
appDelegate.tabBarController.delegate=self;
UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, -20, 320, 600)];
viewControllers = [[NSMutableArray alloc] init];
MainViewController *view1 = [[MainViewController alloc] init];
UINavigationController *nav1=[[UINavigationController alloc]initWithRootViewController:view1];
[viewControllers addObject:nav1];
ViewController1 *view2 = [[ViewController1 alloc] init];
UINavigationController *nav2=[[UINavigationController alloc]initWithRootViewController:view2];
[viewControllers addObject:nav2];
[appDelegate.tabBarController setViewControllers:viewControllers];
appDelegate.tabBarController.tabBarController.view.frame=CGRectMake(0, 0, 320, 480);
[view addSubview:appDelegate.tabBarController.view];
[self.view addSubview:view];
UITabBarItem *tabBarItem1 = [appDelegate.tabBarController.tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [appDelegate.tabBarController.tabBar.items objectAtIndex:1];
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:[arraySelectedImages objectAtIndex:0]] withFinishedUnselectedImage:[UIImage imageNamed:[arrayUnselectedImages objectAtIndex:0]]];
[tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:[arraySelectedImages objectAtIndex:1]] withFinishedUnselectedImage:[UIImage imageNamed:[arrayUnselectedImages objectAtIndex:1]]];
How do i do the same using storyboards?
First add a UIViewController then add the UITabBar and add contraint if you use autolayout.
Don't forgot to connect delegate.
Hope that will help.

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];

Resources