iOS: Programmatically creating UIWindow results in wrong position - ios5

In iOS 5.1.1, I have found that if I create my UIWindow (I'm tired of IB), and set its frame to [UIScreen mainScreen].bounds, the window shows up under the status bar. However if I do the same thing is iOS 6, it appears in the right spot just below the status bar.
CGRect r = [[UIScreen mainScreen] bounds];
self.window = [[UIWindow alloc] initWithFrame: r];
self.detailViewController = [[DetailViewController alloc] init];
self.window.rootViewController = self.detailViewController;
[self.window addSubview: detailViewController.view];
[self.window makeKeyAndVisible];
If however I insert r.origin.y += 20, to move the window below the status bar in iOS 5.1.1, this does not solve the problem and things only get weirder.
What is the proper way to place a UIWindow in iOS when creating it programmatically?
Thanks.

Instead of [[UIScreen mainScreen] bounds] you'll want to use [[UIScreen mainScreen] applicationFrame]. This will be the area of the screen below the status bar (if visible).

I think you probably need to do this:
self.window.windowLevel = UIWindowLevelStatusBar + 1;

Related

How to convert existing height of UIView for iphone 5

Hi I have developed app for iPhone4 now I want to convert the app into iPhone5, I have a UIView named settingsView it is popped up by click of a UIButton. I wanted to know how to adjust the height of the settingsView for iPhone5 .
-(IBAction)settingsButtonChanged:(UIButton *)sender
{
UIImageView *settingsImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"settingsViewImage.png"]];
settingsImage.frame = CGRectMake(25.0, 40.0, 280.0, 370.0);
settingsView.frame = CGRectMake(0.0, 20.0, 280.0, 370.0);
settingsView.backgroundColor = [UIColor clearColor];
[settingsView addSubview:settingsImage];
}
If you use the new autolayout system, you can set constraints that will automatically adjust your view's layout to take advantage of the screen size. For example, if you have several elements near the top of the view, a table in the middle, and some buttons near the bottom, you can add constraints that:
keep the top elements a fixed distance from the top of the view
keep the spacing between top elements fixed
keep the bottom buttons a fixed distance from the bottom of the view
adjust the table's height so that the table grows on a larger screen and shrinks on a smaller one
You can do the math and make the adjustments yourself, but the whole point of the autolayout system is that you don't have to bother -- let the view do it for you.
Maybe, you would like to use two storyboards, one for iPhones 3.5" and the other for iPhones 4". By putting this code on your app delegate, you'll be able to have those storyboards working:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height == 568.0){
//move to your iphone5 storyboard
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"iphoneFiveStoryboard" bundle:[NSBundle mainBundle]];
UIViewController *vc =[storyboard instantiateInitialViewController];
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
}
else{
//move to your iphone4s storyboard
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:[NSBundle mainBundle]];
UIViewController *vc =[storyboard instantiateInitialViewController];
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
}}
// Override point for customization after application launch.
return YES;
}
Now you only need to go to: File -> New... -> File -> User Interface -> Storyboard
And name the storyboard "iphoneFiveStoryboard"...
Now you can remake all the storyboard with 4" views!
Hope you understood, I tried to explain the best I could...
use self.view.frame.size.height, you may have to divide this by something to match your ratio of 320:280 that I noticed for width
-(IBAction)settingsButtonChanged:(UIButton *)sender
{
UIImageView *settingsImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"settingsViewImage.png"]];
settingsImage.frame = CGRectMake(25.0, 40.0, 280.0, self.view.frame.size.height);
settingsView.backgroundColor = [UIColor clearColor];
[settingsView addSubview:settingsImage];
}
You have to check whether the app running on iPhone 4 or iPhone 5 below code lines may help you:-
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 568)
{
// code for 4-inch screen
//for eg:-
[self.view setFrame:CGRectMake(0, 0, 320, 568)];
}
You can try this-
-(IBAction)settingsButtonChanged:(UIButton *)sender
{
UIImageView *settingsImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"settingsViewImage.png"]];
CGRect screenBounds = [[UIScreen mainScreen] bounds];
int height = 0;
if (screenBounds.size.height == 568) {
// Height for iPhone 5 screen
height = 370+ 88; // it may be your desired height
} else {
// Height for iPhone 4 screen
height = 370;
}
settingsImage.frame = CGRectMake(25.0, 40.0, 280.0, height);
settingsView.frame = CGRectMake(0.0, 20.0, 280.0, height);
settingsView.backgroundColor = [UIColor clearColor];
[settingsView addSubview:settingsImage];
}

Dynamic UITextView as the IOS native SMS-app?

I am now trying to develop a chat function like the native one in the iPhone SMS-app. I have followed this guide: https://github.com/HansPinckaers/GrowingTextView how to create a TextView that scale's height depending on the amount of text in it.
Ofcourse I want to integrate this with my existing UIViewController built in storyboard (which is a tableView) instead of customizing a new one by code like being done in this tutorial: https://github.com/HansPinckaers/GrowingTextView/blob/master/example/Classes/GrowingTextViewExampleViewController.m
- (void)loadView {
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view.backgroundColor = [UIColor colorWithRed:219.0f/255.0f green:226.0f/255.0f blue:237.0f/255.0f alpha:1];
.......................................... etc
[self.view addSubview:containerView];
...........................................etc
}
Now, if I wanna replace self.view = [[UIView alloc] code above to be self.view = my viewController from storyboard what shall I type instead? I tried to remove the code instead:
//self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
But I am getting bad access error when trying to add subview code [self.view addSubview:containerView]; to the UIViewController in storyboard since self.view should refer to the storyboard if not being set to something else? It worked when it was set to the customized UIView code:
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view.backgroundColor = [UIColor colorWithRed:219.0f/255.0f green:226.0f/255.0f blue:237.0f/255.0f alpha:1];
So...To sum this up, how do I add the subView (containerView) to my existent UIViewController in storyBoard ? Here is also a pic of my bad access error: http://tinypic.com/view.php?pic=21acqvb&s=6

How can I set my UIView's frame from CGRectMake?

I am new to iOS development, but have had an extensive crash course recently (BNR guid to iOS Programming). I am working on modally adding views to an app. I currently do not have a View Controller, so my AppDelegate's application:didFinishLaunchingWithOptions method looks as follows:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
MyUIView *map = [[MapSurface alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//this one does not work: MyUIView *map = [[MapSurface alloc] initWithFrame:CGRectMake(0, 0, 256, 256)];
[map setBackgroundColor:[UIColor grayColor]];
[[self window] addSubview:map];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
After the view loads, I make a call to add a subview, but I cannot set the frame size using CGRectMake. It doesn't show up on screen. The only thing I have found to work is using the screen size, but this is not what I want. Here is the code for adding this subview:
UIImage *image = [[UIImage alloc] initWithCGImage:img];//img is a CGImageRef
UIImageView *view = [[UIImageView alloc] initWithImage:image];
CGRect b = CGRectMake(0, 0, 256, 256);
//[view setFrame:b];//nothing shows on screen
//[view setBounds:b];//nothing shows on screen
//[view setBounds:[[UIScreen mainScreen] bounds]];//shows the tan area in the graphic below
[view setFrame:[[UIScreen mainScreen] bounds]];//fills the screen
[map addSubView:view];
CGImageRelease(img);
I was setting the bounds attribute, when I should have been setting frame. Making this change fixed the issue.

RootViewController's view is not beeing resized with auto layout

Hi when using autolayout on iOS 6, my "rootView" of my rootViewController doesnt seem to resize properly if at all.
My rootViewController is loaded from a nib and i am adding it to the view-hierarchy like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
RootVC *rootVC = [[RootVC alloc] initWithNibName:#"RootVC" bundle:nil];
self.window.rootViewController = rootVC;
return YES;
}
But when i rotate the simulator the view of rootVC doesnt resize. Since you cant set any constraints to the "rootview" in a xib, I also tried this but without any effect:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
RootVC *rootVC = [[RootVC alloc] initWithNibName:#"RootVC" bundle:nil];
self.window.rootViewController = rootVC;
NSArray *vConst = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|[view]|" options:0 metrics:nil views:#{#"view" : rootVC.view}];
NSArray *hConst = [NSLayoutConstraint constraintsWithVisualFormat:#"H:|[view]|" options:0 metrics:nil views:#{#"view" : rootVC.view}];
[self.window addConstraints:vConst];
[self.window addConstraints:hConst];
return YES;
}
When i log-out the frame-size of the rootvc's view, it's always the portrait-format (w:768, h:1024)
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
NSLog(#"%f, %f", self.view.frame.size.width, self.view.frame.size.height);
}
could someone please tell me what i'm missing here? i've wasted hours with this problem and got nowhere^^
thx
It seems that a lot of things can go wrong with the autolayout feature iOS 6. Ambiguities in the constraints and failing to disable the translatesAutoresizingMaskIntoConstraints in your view controllers can cause much conflict. So ensure there is no ambiguities in the constraints by calling the autolayoutTrace method in lldb: po [[UIWindow keyWindow] _autolayoutTrace]
If there are ambiguities, you must resolve them.

How do I change the color scheme of my iPhone GUI?

I want to customize my GUI for the iPhone in monodevelop but I can't seem to find resources that will show me how to do it. How can I change the background color (load a picture/logo) and change the navigation bar color etc.? Thanks in advance! (What is the programatic way to do this?)
You can change the navigation bar color using the following code:
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
YourViewController *viewController = [[YourViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] \
initWithRootViewController:viewController];
[viewController release];
[[navigationController navigationBar] setTintColor:[UIColor blackColor]];
[window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
You can set the background color (I guess that you want to set the background color of UIView)
like this
UIView *mainView = [[UIView alloc] init];
[mainView setBackgroundColor:[UIColor whiteColor]];
Hope it helps! ;)