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];
}
Related
I added an UIWindow with a subView of an UIView to current key window.
CGFloat width = 35;
self.window = [[UIWindow alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - width, width, width)];
self.window.backgroundColor = [UIColor yellowColor];
self.window.windowLevel = UIWindowLevelStatusBar;
self.window.hidden = NO;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, width)];
view.backgroundColor = [UIColor redColor];
[self.window addSubview:view];
And when I called
[[UIApplication sharedApplication] setStatusBarHidden:sender.selected withAnimation:UIStatusBarAnimationSlide];
In iOS7, after the window has been added, the window's height changed automatically to the screen's height
So what may cause this problem?
I've successfully hooked up several popover views in interface builder. I've tried to make the background black by setting the view background color to black, but I am still getting a white arrow on the popover itself and white artifacts on the 4 rounded corners.
Please see the screenshot.
What can I do to make the background totally black in interface builder?
I appreciate the answer below but I still can't quite get it to work - here is my code:
// Show the satellite Ephemeris
if ( itemIndex == 1 ) {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
NSLog(#"This is an iPad - Creating popover!");
EphemerisDataView *ephemView = [[EphemerisDataView alloc] init];
UIPopoverController *popOver = [[UIPopoverController alloc] initWithContentViewController:ephemView];
[popOver setPopoverBackgroundViewClass:[DDPopoverBackgroundView class]];
[popOver.popoverBackgroundViewClass setTintColor:[UIColor blackColor]];
CGSize size = CGSizeMake(320, 480); // size of view
popOver.popoverContentSize = size;
[popOver presentPopoverFromRect:self.popOverAnchor.bounds inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
else {
NSLog(#"This is not an iPad - Performing segue...");
// Show the next view
[self performSegueWithIdentifier:#"Ephemeris" sender:self];
}
}
You cannot customize that arrow by IB. You will need a totally customized popover for you. Plenty of them are available on git. But If you want to use default one then it is not possible to customize that arrow as well.
Do it like this:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
NSLog(#"This is an iPad - Creating popover!");
UIStoryboard *storyboard = [UIStoryboard storyboardWithName: #"MainStoryboard_iPad" bundle:[NSBundle mainBundle]];
EphemerisDataView *ephemView = [storyboard instantiateViewControllerWithIdentifier:#"EphemerisDataView"];
UIPopoverController *popOver = [[UIPopoverController alloc] initWithContentViewController:ephemView];
self.customPopoverController = popOver;
[popOver setPopoverBackgroundViewClass:[DDPopoverBackgroundView class]];
[popOver.popoverBackgroundViewClass setTintColor:[UIColor blackColor]];
popOver.delegate = self;
CGSize size = CGSizeMake(320, 480); // size of view
popOver.popoverContentSize = size;
[popOver presentPopoverFromRect:self.popOverAnchor.bounds inView:self.popOverAnchor
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
else {
NSLog(#"This is not an iPad - Performing segue...");
// Show the next view
[self performSegueWithIdentifier:#"Ephemeris" sender:self];
}
I am having issues with the iPhone 5 screen size. I have created two xib files (MainWindow.xib and MainWindowiPhone5.xib) so that my app is supported with both screen sizes.
I've been trying to code it on my AppDelegate.m, but it does not work.
Here's my AppDelegate code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
UIStoryboard *storyBoard;
CGSize result = [[UIScreen mainScreen] bounds].size;
CGFloat scale = [UIScreen mainScreen].scale;
result = CGSizeMake(result.width * scale, result.height * scale);
if(result.height == 1136){
storyBoard = [UIStoryboard storyboardWithName:#"MainWindowiPhone5" bundle:nil];
UIViewController *tabBarController = [storyBoard instantiateInitialViewController];
[self.window.rootViewController = self.tabBarController];
}
}
return YES;
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
// Set the tab bar controller as the window's root view controller and display.
self.window.rootViewController = self.tabBarController;
tabBarController.moreNavigationController.navigationBar.tintColor = [UIColor blackColor];
[self.window makeKeyAndVisible];
return YES;
}
How do can I get both xib to work on my code?
Please help. Thanks.
Conditions should be ::
For iPhone-4S or older Screen :
if ([[UIScreen mainScreen] bounds].size.height == 480)
{
---- Your Code ----
}
For iPhone-5 Screen :
if ([[UIScreen mainScreen] bounds].size.height == 568)
{
---- Your Code ----
}
Hopefully... It may be help you.
Thanks.
Add this code in your initialization:
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
if([UIScreen mainScreen].bounds.size.height == 568.0)){
//move to your iphone5 storyboard
[UIStoryboard storyboardWithName:(NSString *) bundle (NSBundle *)];
}
else{
//move to your iphone4s storyboard
[UIStoryboard storyboardWithName:(NSString *) bundle (NSBundle *)];
}
}
I have a View based application for iPhone/iPod that plays audio, having a VU meter showing the input dB level. This VU meter is animated using Core Animation.
The app runs fine, with the VU meter working great, having just one single View. But now, sadly, that I have inserted a Tab bar controller, to have another View with some internet content, I can't get the VU meter to work on my first View, as before.
The app crashes (receiving the EXC_BAD_ACCESS message) because the UIView (that contains the VU meter .png file for Core Animation) is not being properly inserted during launching time as it did before as a View only application.
Here is my code, in the myAppDelegate.m file:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
PlayerViewController *newController = [[PlayerViewController alloc] initWithNibName: #"PlayerView"
bundle: [NSBundle mainBundle]];
self.viewController = newController;
[newController release];
UIView *controllerView = [self.viewController view];
[self.window addSubview: controllerView];
[self.viewController addBargraphToView: controllerView];
[self.window makeKeyAndVisible];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
And here is my PlayerViewController.m file, where I have the addBargraphToView method containing my image to be inserted. It remains invisible until the VU meter starts working:
- (void) addBargraphToView: (UIView *) parentView {
// static image for showing average level
UIImage *soundbarImage = [[UIImage imageNamed: #"vu-meter.png"] retain];
// background colors for generated image for showing peak level
self.peakClear = [UIColor clearColor];
self.peakGray = [UIColor lightGrayColor];
self.peakOrange = [UIColor orangeColor];
self.peakRed = [UIColor redColor];
levelMeter = [CALayer layer];
levelMeter.anchorPoint = CGPointMake (0.0, 20.0); // anchor to halfway up the left edge
levelMeter.frame = CGRectMake (15, 200, 0, kLevelMeterHeight); // set width to 0 to start to completely hide the bar graph segements
levelMeter.contents = (UIImage *) soundbarImage.CGImage;
peakLevelMeter = [CALayer layer];
peakLevelMeter.frame = CGRectMake (41, 233, 0, kLevelMeterHeight);
peakLevelMeter.anchorPoint = CGPointMake (0.5, 10.0);
peakLevelMeter.backgroundColor = peakGray.CGColor;
peakLevelMeter.bounds = CGRectMake (0, 0, 0, kLevelMeterHeight);
peakLevelMeter.contentsRect = CGRectMake (0, 0, 1.0, 1.0);
[parentView.layer addSublayer: levelMeter];
[parentView.layer addSublayer: peakLevelMeter];
[soundbarImage release];
}
How can I solve this problem? Should I insert the UIView image at some other point, and not during the application's launch time?
Many thanks for your help, dear friends!
Move this call:
[self.viewController addBargraphToView: controllerView];
To -viewDidLoad inside your PlayerViewController implementation:
- (void) viewDidLoad
{
[super viewDidLoad];
[self addBargraphToView: self.view];
}
Edit
OK, I think the problem lies in double adding viewControllers. Make this the content of your application:didFinishLaunchingWithOptions: method:
PlayerViewController *newController = [[PlayerViewController alloc] initWithNibName: #"PlayerView"
bundle: [NSBundle mainBundle]];
self.viewController = newController;
[newController release];
self.tabBarController.viewControllers = [NSArray arrayWithObjects: newController, yourInterntContenViewController, nil];
[self.window addSubview: self.tabBarController.view];
[self.window makeKeyAndVisible];
return YES;
I'm trying to do the following:
alt text http://img101.imageshack.us/img101/7396/33959221.png
I need to do a lot of custom things, so I subclassed everything.
alt text http://img248.imageshack.us/img248/4201/49422483.png
I'm having issues with autoresize and positioning. For instance the UIToolBar is not at the correct position.
So here is the question:
If you don't use UITableViewController or any other Controller, that takes care of all the positioning and sizing, then you have to do it yourself.
Can someone someone tell me how this works? What do I need to understand?
---- UPDATE:
My custom goals are actually not that important as my question is actually about something else. But OK. So all "elements" like the TableView, the Toolbar and the TabBar will have custom backgrounds and use custom buttons. The TableView will be GroupedStyle and the groups can be collapsed.
My question is how do I have to structure my code especially regarding positioning and autoresizing in order to achieve what you can see in picture #1.
This is how it's structured right now:
Application Delegate:
- (void) loadView {
RootViewController *rootView = [[RootViewController alloc] init];
[window addSubview:rootView.view];
[window makeKeyAndVisible];
RootViewController:
- (void) loadView {
self.theView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.theView.autoresizesSubviews = YES;
self.theView.autoresizingMask = (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
self.view = self.theView;
ChildViewController *childView = [[ChildViewController alloc] init];
[self.view addSubview:childView.view];
ChildViewController:
- (void) loadView {
self.theView = [[UIView alloc] init];
self.view = self.theView;
}
- (void) viewDidLoad {
self.view.frame = [[UIScreen mainScreen] bounds];
CGRect toolbarBounds = self.theToolbar.bounds;
CGRect viewBounds = self.view.bounds;
CGRect tableViewBounds = CGRectMake(0, CGRectGetHeight(toolbarBounds), CGRectGetWidth(viewBounds), CGRectGetHeight(viewBounds) - CGRectGetHeight(toolbarBounds));
self.view.autoresizingMask = (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
self.theTableView = [[MyTableView alloc] initWithFrame:tableViewBounds
style:UITableViewStyleGrouped
parent:self];
[self.view addSubview:self.theToolbar];
[self.view addSubview:self.theTableView];
}
MyToolBar:
- (id)initWithParent:(id)theParent {
if ((self = [self init])) {
self.parent = theParent;
CGRect rectArea = CGRectMake(0, 0, CGRectGetWidth([[[self parent] view] bounds]), 44);
[self setFrame:rectArea];
self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self loadButtons];
}
return self;
}
So this is how the positioning is structured at the moment (messy). It actually works, BUT I have a ModalView which is handled by the ChildViewController. When I have the iPad in Portrait view and I pop up the modal view and dismiss it, then everything works. But when I do the same on Landscape view it rotates the hole view... the ChildViewController by 90 degrees when dismissing the modal view. Very strange. When I load directly ChildViewController in my AppDelegate, then again everything works fine.
You're missing the UIViewAutoresizingFlexibleBottomMargin option in the autoresize mask of your toolbar, for instance.