UIView change background color to image - iphone

I managed to successfully change some of my UIView background to a custom image using
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"myCustomBackground.jpg"]];
But it does not work on a particular view in a controller that is presented modally as such,
LoginViewController *loginViewController = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:[NSBundle mainBundle]];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:loginViewController];
navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
navController.navigationBar.barStyle = UIBarStyleBlack;
[self presentModalViewController:navController animated:NO];
[loginViewController release];
[navController release];
It seems like self.view inside this controller is behind the view it was showing.
EDIT
LoginViewController.m
- (void)viewDidLoad {
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"myCustomBackground.jpg"]];
}
self.view in this case seems to be hidden behind the current view that it's displaying. I did not add any subView beforehand.

As you mentioned in your comments, your view contains UIScrollView, UILabel and UIButton.
You should set to all this subviews property backgroundColor = [UIColor clearColor];. Hopefully that would help.
If you are editing subview in IB then you should find property Background in Attributes Inspector and select Clear Color (for all subviews).

Related

How to make navigation bar black in modal view in ios

I made a modal view. I don't want default color. How can I put its navigation to black color?
Here is my code which doesn't work:
EditBillItemModalViewController *editBillItemvc = [[EditBillItemModalViewController alloc] init];
editBillItemvc.anItem = anItem;
editBillItemvc.navigationController.navigationBar = [UIColor blackColor];
editBillItemvc.onOKButtonClickedCallBack = ^(Item item){ [aBill.listOfOrderedItem replaceObjectAtIndex:bvc.selectedItemIndex withObject:item]; [(UITableView)bvc.view reloadData]; [self setnewTotalAfterEditBillItem]; };
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:editBillItemvc];
[self presentModalViewController:navController animated:YES];
Assuming it has a UINavigationController you can do:
modalView.navigationController.navigationBar.tintColor = [UIColor blackColor];
EDIT after code added:
From your code it looks like here is your succession of events:
Instantiate view controller
Set bar tintColor to black
Instantiate nav controller
Present nav controller as modal view
Obviously this isn't going to work as you're changing the tint color before you're instantiating the nav controller.
Thus, you need to either move step 2 to between 3 and 4 or, even easier after this line:
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:editBillItemvc];
Just put:
navController.navigationBar.tintColor = [UIColor blackColor];

Change the background color of UINavigationBar

I'd like to change the background color of an UINvaigationBar and I used tintColor property but it's not changing the color. You can checkout the code below.
In my Appdelegate I create a UITabBarController
tabbar = [[UITabBarController alloc] init];
Then I create a UINavigationController and attach my UITableViewController to it
UINavigationController *myNavigation = [[UINavigationController alloc] initWithRootViewController:myViewController];
Then attached the UINavigationControllers to my tabbar like this
[tabbar setViewControllers:viewControllers]; //viewControllers is an array of UINavigationControllers
I tried setting the property tintColor in myAppdelegate like this
[[myNavigation navigationBar] setTintColor:[UIColor redColor]];
But this didn't work as expected and so I tried the same in my ViewController
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
self.navigationItem.title = #"Test";
}
And still nothing happens. Please checkout the image below to see how navigation bar looks now.
Appreciate your help.
Cheers
Jugs
As a work around you may can use image on place of tint color please check below code for same.
if ([myNavigation respondsToSelector:#selector(setBackgroundImage:forToolbarPosition:barMetrics:)]) {
[myNavigation setBackgroundImage:[UIImage imageNamed:BAR_IMAGE_NAME] forbarMetrics:UIBarMetricsDefault];
}else {
[myNavigation insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:BAR_IMAGE_NAME]] autorelease] atIndex:0];
}

Problem in showing subview as popover

I want to show the subview of a view in popover.
To elaborate,
I have a mainViewController.
I hava a subview 'songListView' in this mainViewController.
I have a button titled 'list' in the mainViewController' itself.
I want to show the songListView in popover on clicking the button 'list'.
So, how should I do this.
You could use the below code as a reference for showing PopOver from a UIButton
-(void) buttonAction:(id)sender {
//build our custom popover view
UIViewController* popoverContent = [[UIViewController alloc]
init];
UIView* popoverView = [[UIView alloc]
initWithFrame:CGRectMake(0, 0, 200, 300)];
popoverView.backgroundColor = [UIColor greenColor];
popoverContent.view = popoverView;
//resize the popover view shown
//in the current view to the view's size
popoverContent.contentSizeForViewInPopover =
CGSizeMake(200, 300);
//create a popover controller
self.popoverController = [[UIPopoverController alloc]
initWithContentViewController:popoverContent];
//present the popover view non-modal with a
//refrence to the button pressed within the current view
[self.popoverController presentPopoverFromRect:popoverButton.frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
//release the popover content
[popoverView release];
[popoverContent release];
}
My problem is solved.
I just created another View Controller class, i.e. 'TempPopoverView'.
Then I set the view of this TempPopoverView equal to the subview of my MainView
Here is the code snippet from my code:
TempPopoverView *viewController = [[TempPopoverView alloc]initWithNibName:#"TempPopoverView" bundle:nil];
[self. songListView setHidden:NO];//songListView is subview of MainView
viewController.view=self. songListView;
UINavigationController *navCont = [[UINavigationController alloc]initWithRootViewController:viewController];
navCont.navigationBar.tintColor = [UIColor colorWithRed:.102 green:.102 blue:.102 alpha:1];
[self showPopOverController:navCont andFrame:sender.frame andInView:self.view];//self.view is the MainView
[viewController release];
[navCont release];
U can use presentModalViewController on your main view.
For example
SongListView *songList = [[SongListView alloc] init];
[self presentModalViewController: songList animated: YES];
There are different animations possible as well.
as simple as you think to add subview in self.view
[popoverController.contentViewController.view addSubview:yourselfobject];

NavigationBar is not appearing on my ViewController

I have a UIViewController that is presented modally. The UIViewController is inside of a UINavigationController.
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
saveButton = [[UIBarButtonItem alloc] initWithTitle:#"Save"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(saveButtonClicked:)];
self.navigationItem.rightBarButtonItem = saveButton;
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:(102.0/255.0) green:(20.0/255.0) blue:(11.0/255.0) alpha:1];
self.title = #"Login";
//toolbar.tintColor = [UIColor colorWithRed:(102.0/255.0) green:(20.0/255.0) blue:(11.0/255.0) alpha:1];
}
Why isn't my navbar appearing with the text Login and a save button to the right?
It sounds like you may be presenting the view controller instead of the navigation controller. Your code to present the view controller you've described should look something like this:
MyViewController *viewController = [[MyViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
[self presentModalViewController:navController animated:YES];
[viewController release];
[navController release];
You set the properties after the nib is loaded, but you probably need to do it after/while it is pushed.

Hiding UIToolBar for child views of UITableViewController

My main controller is a subclass of UITableViewController with a UIToolBar at the bottom and when a row is selected, I'd like to display another view without the toolbar. How can I hide the UIToolBar in the child view? Right now, it's present throughout all child views unless they're created as modal.
Toolbar is created in RootController:
self.toolbar = [[UIToolbar alloc] init];
// add tool bar items here
[self.navigationController.view addSubview:toolbar];
RootController displays its child views as such:
UIViewController *controller = [[UIViewController alloc] init...]
[self.navigationController pushViewController:controller animated:YES];
RootController is instantiated as such in the app delegate's applicationDidFinishLaunching:
RootController *rootcontroller = [[RootController alloc] initWithStyle:UITableViewStyleGrouped];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:rootcontroller];
[rootcontroller release];
[window addSubview:[self.navigationController view]];
If I add the toolbar to [self.view] within RootController instead of navigationController's view, the toolbar disappears alltogether..
You can try hiding the toolbar before you display our child view with 'toolbar.hidden = YES' and then in your viewWillAppear method, show it again with 'toolbar.hidden = NO'.
Another alternative would be using "removeFromSuperview"
[toolbar removeFromSuperview];
Then use viewDidAppear method in the view where u wanna re-show the toolbar.
It works better than viewWillAppear since the toolbar is added after the view is showed.
(For viewWillAppear, toolbar is added during the transition so it is kinda awkward.)
I got it working with this
[toolbar removeFromSuperview];
Check this
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//Initialize the toolbar
toolbar = [[UIToolbar alloc] init];
toolbar.barStyle = UIBarStyleDefault;
//Set the toolbar to fit the width of the app.
[toolbar sizeToFit];
//Caclulate the height of the toolbar
CGFloat toolbarHeight = [toolbar frame].size.height;
//Get the bounds of the parent view
CGRect rootViewBounds = self.parentViewController.view.bounds;
//Get the height of the parent view.
CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds);
//Get the width of the parent view,
CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds);
//Create a rectangle for the toolbar
CGRect rectArea = CGRectMake(0, rootViewHeight - toolbarHeight, rootViewWidth, toolbarHeight);
//Reposition and resize the receiver
[toolbar setFrame:rectArea];
//Create a button
UIBarButtonItem *infoButton = [[UIBarButtonItem alloc]
initWithTitle:#"back" style:UIBarButtonItemStyleBordered target:self action:#selector(info_clicked:)];
[toolbar setItems:[NSArray arrayWithObjects:infoButton,nil]];
//Add the toolbar as a subview to the navigation controller.
[self.navigationController.view addSubview:toolbar];
[[self tableView] reloadData];
}
- (void) info_clicked:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
[toolbar removeFromSuperview];
}