Subviews in UIView Hierarchy not displaying subviews - iphone

When this Action is executed it displays totally black view screen with toolbar
- (void)displayviewsAction:(id)sender
{
self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];
self.view.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:toolbar];
UIView *viewController = [[UIView alloc] init];
UIView *secondController = [[UIView alloc] init];
[self.view insertSubview:viewController atIndex:1];
[self.view insertSubview:secondController atIndex:2];
NSLog(#"%d", [[self.view subviews] indexOfObject:viewController]); // Is 1
NSLog(#"%d", [[self.view subviews] indexOfObject:secondController]); // Is 2
[self.view bringSubviewToFront:viewController];
NSLog(#"%d", [[self.view subviews] indexOfObject:viewController]);
[self.view sendSubviewToBack:viewController];
NSLog(#"%d", [[self.view subviews] indexOfObject:viewController]);
[self.view bringSubviewToFront:secondController];
NSLog(#"%d", [[self.view subviews] indexOfObject:secondController]);
SecondViewController * secondController = [[[SecondViewController alloc] init]autorelease];
secondController.view.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:secondController.view];
[self.view addSubview:toolbar];
}
Any ideas why the view is appearing as totally black with toolbar.

The default background color of views is nil, which is a transparent background.
If you have nothing in any of the views and set no background color for any of them, the default background of the window or the device will show through. I didn't see it in the documentation, but I am assuming it is black.
Also, you have defined UIView pointers, but named them with the word "controller". This can contribute to mistakes since a UIViewController is an entirely different object. The UIViewController contains a UIView and interacts with it, but it is not a UIView, nor does it inherit from one.

Related

supporting iphone 5 screen size

Note: Everything worked fine on the older screen sizes however on the new iphone screens (640x1136) things are way to far down
here is the App Delegate initing and showing myRootViewController
myRootViewController = [[RootViewController alloc] initWithNibName:#"RootViewController" bundle:[NSBundle mainBundle]];
[myRootViewController.view setFrame:[[UIScreen mainScreen] applicationFrame]];
//NSLog(#"rootviewcontroller1 frame %#", NSStringFromCGRect(myRootViewController.view.frame));
//OUTPUTS: {{0, 0}, {320, 460}}
[window addSubview:myRootViewController.view];
The RootViewController sets the frame for the 'navigationController' and a few other views before adding them to its view.
//NSLog(#"ogtest rootviewcontroller frame2 %#", NSStringFromCGRect(self.view.frame));
//OUTPUTS: {{0, 20}, {320, 548}}
[navigationController.view setFrame:CGRectMake(0, 0, 320,528)];
//I have tried multiples variations including not setting it.
loadingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[loadingView setBackgroundColor:[UIColor clearColor]];
UILabel *_loadingLable = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 80.0f, 20.0f)];
_loadingLable.backgroundColor = [UIColor clearColor];
_loadingLable.textColor = [UIColor whiteColor];
_loadingLable.text = #"Loading...";
[_loadingLable setCenter:CGPointMake(181.0f, 240.0f)];
activityIndicatior = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[activityIndicatior setCenter:CGPointMake(120.0f, 240.0f)];
RoundedView *_roundedRectangle = [[RoundedView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 140.0f, 50.0f) roundedCorner:RoundedCornersTypeALL];
_roundedRectangle.center = CGPointMake(160.0, 240.0);
_roundedRectangle.rectColor = [UIColor blackColor];
_roundedRectangle.alpha = 0.7;
[loadingView addSubview:_roundedRectangle];
[loadingView addSubview:activityIndicatior];
[loadingView addSubview:_loadingLable];
[_loadingLable release];
[_roundedRectangle release];
[loadingView setHidden:YES];
[self.view addSubview:[navigationController view]];
[self.view addSubview: loadingView];
You can see from the image below that the grey bar is there for the nav bar. The text "Tuesday 30 Apr...." with the arrow buttons should occupy that grey area and not 2 cells from the top.
If you find yourself setting the frame of your navigation controller / navigation bar, you're doing something wrong.
Most of the time, you don't even need to set the frame of your view controller's view either.
Your app delegate needn't be any more complicated than this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self setWindow:[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]];
[[self window] setRootViewController:[[UINavigationController alloc] initWithRootViewController:[[RootViewController alloc] init]];
[self.window makeKeyAndVisible];
return YES;
}
It will most likely be a setting in your RootViewController nib. For the first view in the "Objects" list on the left, you have probably set the Top Bar property.
I ended up creating separate xib files with
_iPhone_Retina
at end of the file name
Which also called for statements like this
if (IS_IPHONE_RETINA) {
View = [[View alloc] initWithNibName:#"View_iPhone_Retina" bundle:[NSBundle mainBundle]];
}else{
View = [[View alloc] initWithNibName:#"View" bundle:[NSBundle mainBundle]];
}
and IS_IPHONE_RETINA is defined in the projectname.pch file as
#define IS_IPHONE_RETINA ([[UIScreen mainScreen] bounds].size.height == 568 )

How to show UIToolbar in the UIView

MainViewController have UIToolbar and when button is pressed on the UIToolbar, it displays view i want to show the UIToolbar in all my views.
I set this command in display view action
[self.navigationController setToolbarHidden:NO animated:NO];
but this statement is not helping in displaying UIToolbar in the view
- (void)displayviewsAction:(id)sender
{
self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];
[self.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
PageOneViewController *viewController = [[[PageOneViewController alloc] init]autorelease];
[self.view addSubview:viewController.view];
[self.navigationController setToolbarHidden:NO animated:NO];
}
Please suggest how to show UIToolbar in views.
Thanks for help.
Added
[self.view addSubview:toolbar];
This helped in solving the issue.

Gap between statusbar and view

Dealing with this issue for last so many days when button is pressed on the Mainviewcontroller it plays audiofile as well as displays View with UIToolbar at the bottom of the View.
UIView has UIImage and UIText
My issue is that when View is loaded it shows gap between status bar and the loaded view and when infobutton is pressed from the UIToolbar on the view it presents Modalviewcontroller and when this Modalviewcontroller is dismissed back to the View it shows same gap between the status bar and the View.
- (void)displayviewsAction:(id)sender
{
self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];
self.view.superview.frame = CGRectMake(0, 0, 320, 480);
PageOneViewController *viewController = [[[PageOneViewController alloc] init]autorelease];
[self.view addSubview:viewController.view];
[self.view addSubview:toolbar];
}
Please anyone knows how to fix this issue. Appreciate your help.
Thanks
After struggling for 2 days got the solution for my problem. It was very simple.
I made these changes
- (void)displayviewsAction:(id)sender
{
//self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];
//self.view.superview.frame = CGRectMake(0, 0, 320, 480);
PageOneViewController *viewController = [[[PageOneViewController alloc] init]autorelease];
viewController.view.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:viewController.view];
[self.view addSubview:toolbar];
}
Now there is no gap. It is working fine.

How to resize to fullscreen when touched the view that in TabBarController based app?

Here're some view controller I have: tabBarController, navigationBarController, viewController, and imageView.
The hierarchy is that: tabBarController is the root controller, it includes navigationBarController. And the viewController(which is include the imageView) is pushed in by navigationBarController.
So the question is, when I touched the imageView, how to resize it to fullscreen with smooth animation. Note, I don't want to hide NavigationBar & TabBar.
I tried it by setFrame, however, it is covered by navigationBarController and tabBarController.
I tried presentModalViewController: but it lose continuity.
I also tried to reset rootViewController to viewController, but the resulted with a broken animation. :(
Thanks for any suggestion!
Test code
UIViewController * viewController = [[UIViewController alloc] init];
[viewController.view setFrame:CGRectMake(0.0f, 0.0, 320.0f, 480.0f)];
[viewController.view setBackgroundColor:[UIColor redColor]];
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
[imageView setBackgroundColor:[UIColor yellowColor]];
[imageView setUserInteractionEnabled:YES];
[viewController.view addSubview:imageView];
[imageView release];
UINavigationController * navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
[viewController release];
UIViewController * anotherTabViewController = [[UIViewController alloc] init];
[anotherTabViewController.view setFrame:CGRectMake(0.0f, 0.0, 320.0f, 480.0f)];
[anotherTabViewController.view setBackgroundColor:[UIColor blueColor]];
UITabBarController * tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:navigationController, anotherTabViewController, nil];
[navigationController release];
[anotherTabViewController release];
[self.window setRootViewController:tabBarController];
Here I write a simple code for testing. Now don't care about the touch action. I wonder how to set the imageView to totally fullscreen(width:320, height:480). But it's clipped by navigation bar and tab bar. I know the frame is relative to its super view. How to deal with this issue?
try to add img to you window, when you need to resize it to full screen:
UIImageView *img; //create new image
img = yourImageView;
[img setOrigin:CGPointMake(yourImageView.frame.origin.x, yourImageView.frame.origin.y+90)];
//window size is bigger, so to set an image frame visionary at same place, you need to set origin point lower
MOEAppDelegate *sharedAppDelegate = (MOEAppDelegate *)[[UIApplication sharedApplication] delegate];
[sharedAppDelegate.window addSubview:img];
[UIView animateWithDuration:1
animations:^{
[img setFrame:CGRectMake(0,
0,
320,
480)];
}];
Note, that your self.view's frame automatically resized when you adding tabBar or navigationBar, and in -(void)viewDidLoad it is NOT resized jet!

Can you add a UIView above a UINavigationBar?

I have seen it done on some apps, where the navigation bar is actually smaller than the default 44px, and there is a UIView (which has functionality) above the nav bar...
I want more than a custom background image, which I did manage to figure out how to do, but I dont know where to start getting something like this done.
Any help is greatly appreciated :)
Mark
I found a sort of way to do this:
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 20.0f, 320.0f, 32.0f)];
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
UIImageView *back = [[UIImageView alloc] initWithImage: [UIImage imageNamed:#"logo.png"]];
[back setFrame: CGRectMake(0, 0, 320, 20)];
[tempView addSubview: back];
[[self view] addSubview: tempView];
[[self view] addSubview: navBar];
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle: #"Controls"];
[navBar pushNavigationItem:navItem animated:NO];
which seems to do the trick, although I cannot seem to figure out how to get this 'into' the navigationController so that back buttons work, at the moment I have to manually insert a leftBarButtonItem into the navItem, the back button never seems to show...
Yes you can,
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 20.0f, 320.0f, 32.0f)];
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
navBar.navigationBar.layer.zPosition =-1;
self.view insertSubview:navBar atIndex:[[self.view subviews] count]];
[self.view insertSubview:tempView atIndex:[[self.view subviews] count]];