How to make a UITableViewController transparent on the top? - iphone

In my iPhone application I have presented controller:
ShareResourceItemViewController* controller = [[ShareResourceItemViewController alloc] initWithSharedResourceItem:[selectedItems objectAtIndex:0]];
[self presentViewController:controller animated:YES completion:nil];
Then in this controller I want to make it transparent on the top. I tried:
-(void)viewDidLoad{
[super viewDidLoad];
[self performSelector:#selector(test) withObject:nil afterDelay:3];
}
-(void)test{
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.opaque = NO;
self.tableView.backgroundView = nil;
}
The background was cleaned but controller is not transparent and I can not see previous controller? How can I fix this?

Make the tableview cells background color also to clear color.

Try this.
self.view.backgroundColor = [UIColor clearColor];

I think that if you want to see your previous View Controller, you want to not present a new view controller, but rather, put a new UIView on top of your View Controller. View Controllers can't be transparent or not, but views can. Give it a try:
ShareResourceItemViewController* controller = [[ShareResourceItemViewController alloc] initWithSharedResourceItem:[selectedItems objectAtIndex:0]];
[self.view addSubview:controller.view];

Related

iOS spacing issue with translucent navigation bar

I'm currently having a UIViewcontroller with a 3-segment UISegmentedControl that when clicked switches view controllers displayed. The navigation bar and tab bar of this view are translucent.
I initialize the view like this:
- (void)viewDidLoad {
[super viewDidLoad];
[self setAutomaticallyAdjustsScrollViewInsets:YES];
self.segControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"View 1",#"View 2",#"View 3",nil]];
[self.segControl setTintColor:[[ConstantsSingleton sharedConstants] default_bg_Color]];
[self.segControl setSegmentedControlStyle:UISegmentedControlStyleBar];
[self.segControl addTarget:self action:#selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];
[self.segControl setSelectedSegmentIndex:0];
self.navigationItem.titleView = self.segControl;
//Setting up the first viewcontroller
UIViewController *vc = [self viewControllerForSegmentIndex:self.segControl.selectedSegmentIndex];
[self addChildViewController:vc];
vc.view.frame = self.contentView.bounds;
[self.contentView addSubview:vc.view];
self.currentViewController = vc;
}
The contentView is a IB defined UIView with 0 leading and trailing on all sides (so basically it fills the parent view).
I switch viewcontrollers the following way:
-(void)segmentChanged:(UISegmentedControl *)sender {
UIViewController *vc = [self viewControllerForSegmentIndex:sender.selectedSegmentIndex];
[self addChildViewController:vc];
[self transitionFromViewController:self.currentViewController toViewController:vc duration:0.5 options:UIViewAnimationOptionTransitionFlipFromRight animations:^{
vc.view.frame = self.contentView.bounds;
[self.currentViewController.view removeFromSuperview];
[self.contentView addSubview:vc.view];
} completion:^(BOOL finished) {
[vc didMoveToParentViewController:self];
[self.currentViewController removeFromParentViewController];
self.currentViewController = vc;
}];
self.navigationItem.title = vc.title;
}
Now whenever I run this with a opaque navigation bar and tab bar this works fine, but whenever I try to use a translucent navigation bar and/or tab bar only the first view gets resized/its insets adjusted properly to not be behind the translucent navigation bar and/or tab bar. The second and third view will still appear behind them when they appear on screen.
It doesn't matter which viewcontroller is set as the first viewcontroller, all lead to the same behaviour.
What could be causing this issue and is there a way to solve this without resolving to manual contentinset adjustments.
I would suggest to enable the option Extend Edges in the property inspector of you're view.
Here's a good stackoverflow post differentiating the various layout settings for iOS 7 and up:
Explaining difference between automaticallyAdjustsScrollViewInsets, extendedLayoutIncludesOpaqueBars, edgesForExtendedLayout in iOS7

Transparent UITabBarController with changeable view in background

I want to have a UITabBarController, which is has an alpha of 0.5, whose transparency allows you to see a view in the background. The background view has to be accesible and changeable.
I'm able to add the background using this technique: https://gist.github.com/1157542
It's a Category which adds a subview to the UITabBarController, and sends the subview to the back. However, because it's a category, I can't make the subview a property. So I can't really access it.
Is there a way to make this background view more flexible and accessible? So I could, for instance, add other subviews to it easily from any of the tab bar controller's view controllers?
Instead of a category, you should subclass UITabBarController. This will allow you to have finer control over the object. Here's an example of a subclass.
// MPCustomTabBar.h
#interface MPCustomTabBar : UITabBarController
- (void) setBackgroundImage:(UIImage *)image;
#end
// MPCustomTabBar.m
#interface MPCustomTabBar
- (void) setBackgroundImage:(UIImage *)image {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)];
imageView.backgroundColor = [UIColor colorWithPatternImage:i];
[[self view] addSubview:imageView];
[[self view] sendSubviewToBack:imageView];
[[self view] setOpaque:NO];
[[self view] setBackgroundColor:[UIColor clearColor]];
[imageView release];
}
#end
Now you can do all the customization you want, alloc and init your new subclass by something like this:
MPCustomTabBar *bar = [[MPCustomTabBar alloc] init];
A solution to my problem might be simply this..
In my AppDelegate, just before [self.window makeKeyAndVisible];
self.theImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image1.png"]];
[self.tabBarController.view insertSubview:self.theImage atIndex:0];
I can then easily change this image, in any of the tab bar controller's view controllers, like:
AppDelegate *appDelegate= (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.theImage.image = [UIImage imageNamed:#"image2.png"];
.. no categories required.
Here's what worked for me:
Make TabBar transparent (either by Storyboard or programmatically) by setting the Tint- and Background to Clear Color and Opaque to No.
The black background color is actually the window's colour. Assign the image you want to use as the background to the window itself:
UIImage *i = [UIImage imageNamed:#"main_background.png"];
UIColor *c = [[UIColor alloc] initWithPatternImage:i];
[self.window setBackgroundColor:c];

UIView change background color to image

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).

A new view is not visible

I just made a view using CGRectMake but the view is not visible. Here's my code until now:
mainView = [[UIView alloc] initWithFrame:CGRectMake(0,20,320,460)];
mainView.userInteractionEnabled = YES;
mainView.backgroundColor = [UIColor whiteColor];
mainView.alpha = 1.0;
[mainView setHidden:NO];
Is there another property I need to add in order to make it be visible/functional?
You forgot to add view on main view. So do like below.
[self.view addSubview:mainView];
So Just replace whole code with below.
mainView = [[UIView alloc] initWithFrame:CGRectMake(0,20,320,460)];
mainView.userInteractionEnabled = YES;
mainView.backgroundColor = [UIColor whiteColor];
mainView.alpha = 1.0;
[mainView setHidden:NO];
[self.view addSubview:mainView];
You have to add the new view to an existing view. You could use (for example):
[[[UIApplication sharedApplication] keyWindow] addSubview:myNewView];
If you are using a UIViewController, it would be:
[[myViewController view] addSubview:myNewView];
You need to add it as a subview in your UIViewController or you UIApplication keyWindow.
Also, you have to make sure its frame position is inside the parentView application bounds IF the parent view has the clipToBounds property set to YES

Background image for MoreNavigationController

I'd like to place an image behind the tableView in my UITabBarController moreNavigationController. I have tried inserting a subview like so when first setting up the TabBar:
UIImageView* imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background3.png"]];
[self.tabBarController.moreNavigationController.topViewController.view insertSubview:imageView atIndex:0];
But this places the image over the top, presumably because the tableView isn't there at the time. Is there a better time when I can call this in order to have it work properly, or an easier approach?
With some assistance from this question, I figured out how to do this. Basically, the viewController in the moreNavigationController is a single TableView, so adding a background image won't work. What I need to do was to create a new view, add the background image, and then add the moreNavigationController view on top of that. I did this by overriding viewDidLoad in a subclass of UITabBarController, but I expect it could be done elsewhere as well.
- (void)viewDidLoad {
[super viewDidLoad];
UINavigationController *moreController = self.moreNavigationController;
if ([moreController.topViewController.view isKindOfClass:[UITableView class]]) {
UIView* newView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,367)];
UIImageView* imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background3.png"]];
imageView.opaque = NO;
imageView.alpha = 0.4;
[newView addSubview:imageView];
moreController.topViewController.view.backgroundColor = [UIColor clearColor];
moreController.topViewController.view.frame = CGRectMake(0,0,320,367);
[newView addSubview:moreController.topViewController.view];
moreController.topViewController.view = newView;
}
}
You could probably be smarter with the frame sizes, etc, but this works for me. Hopefully it helps someone else too.
Now you can acess backgroundView property from UITableView subclasses .
UIViewController *moreViewController = tabBarController.moreNavigationController.topViewController;
img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"BG_MORE+1.png"]];
//Got some crashs in initialization !! Need to check .
if ([moreViewController.view isKindOfClass:[UITableView class]]) {
UITableView *moreTableView = (UITableView*)moreViewController.view;
[moreTableView setBackgroundView:img];
}
Besides all the dotty mess here, you can use UIView's bringSubviewToFront: and sendSubviewToBack: to organize your subviews. Basically this should help, although if you have more subviews you will need to play around with it a little bit:
[self.tabBarController.moreNavigationController.topViewController.view addSubview:imageView];
[self.tabBarController.moreNavigationController.topViewController.view pushSubviewToBack:imageView];
//or [self.tabBarController.moreNavigationController.topViewController.view bringSubviewToFront:tableView];