My app was designed for iPhone 5, but now I'm adding compatibility for other devices.
I have a UIView, created in Interface Builder. I want to set a new frame for this view. I am checking the screen's width and height at runtime, but it's not changing the view's frame. Here is my code:
//in viewDidLoad:
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
if (screenHeight == 480 && screenWidth == 320) {
bottomView.frame = CGRectMake(0, 60, 320, 420);
}
Goto interface builder tap on view, you want to resize, click on Attribute inspector than click on size and select freeform after that in your viewWillAppear resize your view, you can check size of you view by:-
NSLog(#"view frame ==> %#", NSStringFromCGRect(self.view.frame));
Make a class that returns you a screen frame info.
Create a new File with name FullScreen with UIScreen as sub class
#import <UIKit/UIKit.h>
#interface FullScreen : UIScreen
+ (CGRect)iOS7StyleScreenBounds;
#end
#import "FullScreen.h"
#implementation FullScreen
+ (CGRect)iOS7StyleScreenBounds {
UIScreen *screen = [UIScreen mainScreen]; CGRect screenRect;
if (![screen respondsToSelector:#selector(fixedCoordinateSpace)] && UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
screenRect = CGRectMake(screen.bounds.origin.x, screen.bounds.origin.y, screen.bounds.size.height, screen.bounds.size.width);
} else {
screenRect = screen.bounds;
}
return screenRect;
}
#end
Now import this class in your view controller and call like this:
self.view.frame = [FullScreen iOS7StyleScreenBounds];
Try Putting the code in
- (void)viewDidAppear:(BOOL)animated;
One definite solution which can work is , you should remove your UIView from xib and create it programmatically.Which can be done by the following code:
newView=[[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 430)];
[newView setBackgroundColor:[UIColor yellowColor]];
[self.view newView];
Related
In my previous question I shared my problem with the black background appearing in my app on orientation change: Black background when view rotates on orientation change
I did not manage to solve the problem by following any of the advices I got and I have the feeling that the only way I can avoid the black background is by manually rotating my subviews on orientation change?
One of my subviews is a UILabel which is supposed to cover the entire screen. The rotation is going pretty well using a line of code similar to this one:
myLabel.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(isLandscape ? 90 : 0));
My problem is to make the UILabel adjust to take up the entire screen in landscape mode as well. I have tried to switch height and width of its bounds, but nothing happens.
Any suggestions will be greatly appreciated.
Here are some more code details:
[UIView animateWithDuration:0.5
animations:^{
myLabel.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(isLandscape ? 90 : 0));
}
completion:^(BOOL finished) {
myLabel.bounds = CGRectMake(0, 0, [self screenWidth], [self screenHeight]);
CGFloat fontSize = ((isLandscape ? 0.9649 : 0.9375) * [self screenWidth]) / 2.74;
[myLabel setFont:[UIFont fontWithName:FontName size:fontSize]];
}
];
where
- (CGFloat) screenWidth {
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
return isLandscape ? MAX(screenSize.width, screenSize.height) : MIN(screenSize.width, screenSize.height);
}
- (CGFloat) screenHeight {
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
return isLandscape ? MIN(screenSize.width, screenSize.height) : MAX(screenSize.width, screenSize.height);
}
Perhaps you are calling sizeToFit on the label somewhere?
Try instead to set:
myLabel.adjustsFontSizeToFitWidth = YES;
myLabel.minimumFontScale = 0.1;
This will adjust your labels font size to fit the width of label.
I found out that I was able to adjust the label size once I unchecked the auto layout checkbox.
I am trying to add a subview to a UIScrollView. First I instantiate the view controller from the storyboard, then set the view's frame to the application bounds. When I add the view to the UIScrollView, it is clearly larger than it's supposed to be.
CGRect mainFrame = CGRectMake(0, topButtonHeight, screenWidth, screenHeight);
feelingVC = (FeelingViewController *)[self.storyboard instantiateViewControllerWithIdentifier:#"feelingVC"];
feelingVC.delegate = self;
feelingView = feelingVC.view;
[feelingView setFrame:mainFrame];
[self.scrollView addSubview:feelingView];
I can tell because its background color is extending past where it should be. "Debug View Hierarchy" mode in XCode also confirms this. But, if I inspect the view's frame, it prints out what it should be, not what it actually is.
A view of the same size, but generated completely programatically, works as it should:
mainView = [[UIView alloc] initWithFrame:mainFrame];
mainView.backgroundColor = [UIColor blackColor];
[self.scrollView addSubview:mainView];
I'm not sure what could be causing this issue - I've instantiated other views from their view controllers, also via the storyboard, and set their frames without any problems.
EDIT: This is being called from viewDidLoad of the view controller containing the UIScrollView. screenWidth & screenHeight are calculated as such (they are instance variables):
screenWidth = [UIScreen mainScreen].applicationFrame.size.width;
screenHeight = [UIScreen mainScreen].applicationFrame.size.height;
Try to set the view frame in viewWillAppear method.
viewDidLoad is not a good place to set frames (because it called only once when view getting loaded.), the UI components are not properly configured by the system yet.
Also, prefer to use:
screenWidth = [UIScreen mainScreen].bounds.size.width;
screenHeight = [UIScreen mainScreen].bounds.size.height;
instead of
screenWidth = [UIScreen mainScreen].applicationFrame.size.width;
screenHeight = [UIScreen mainScreen].applicationFrame.size.height;
since bounds has correct positional information taking into consideration the device's orientation in this case.
Or you can just do this, after initializing the mainView inside viewDidLoad: Method
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
mainView.frame = [UIScreen mainScreen].bounds;
}
you can also add this to reconfigure the view frame whenever the subviews are updated:
- (void)viewWillLayoutSubviews { //or viewDidLayoutSubviews
[super viewWillLayoutSubviews];
mainView.frame = [UIScreen mainScreen].bounds;
}
I am developing an app. In the app I wrote the code for a 3.5-inch screen. I took one view controller; in this view controller I want to display two images, one for the top (Imagelogo) and another for the bottom (Image Building). On the 3.5-inch screen there is no problem with the display. But on the 4-inch screen, there is a display problem. The two images are not displayed properly. I don't know how to write the same code for both the 3.5 and 4-inch screen. Can I use macros? Please give me ideas, anyone. I am new to programming. Thanks in advance.
The below is my code.
Viewcontroller.m (3.5 inches screen)
This image will be displayed on top.
imgLogo=[[UIImageView alloc]initWithFrame:CGRectMake(75, 10, 162, 57)];
imgLogo.image=[UIImage imageNamed:#"Logo-01.png"];
[self.view addSubview:imgLogo];
This image will be displayed on bottom
imgBuilding=[[UIImageView alloc]initWithFrame:CGRectMake(0, 320, 320,140 )];
imgBuilding.image=[UIImage imageNamed:#"image-02.png"];
[self.view addSubview:imgBuilding];
You can autoResize feature in the in the interface builder or use it via code.
You can use Apple's Auto Layout system if your app support IOS 5 and above.
You can place the images according the screen size via code
if ([[UIScreen mainScreen] bounds].size.height == 568)
{
//iphone 5 image
}else
{
}
In your "Viewcontroller.h" define float scaleY;
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
{
float scaleY;
}
#end
In your "Viewcontroller.m" class
in your viewDidLoad method after [super viewDidLoad]; put your scaleY value for your different screen size devices in iPhone family.
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
if ([[UIScreen mainScreen] respondsToSelector: #selector(scale)])
{
CGSize result = [[UIScreen mainScreen] bounds].size;
CGFloat scale = [UIScreen mainScreen].scale;
result = CGSizeMake(result.width * scale, result.height * scale);
NSLog(#"Height ........%f",result.height);
if(result.height == 480 || result.height == 960)
{
// iPhone 3.5 inches Screen
scaleY=1.0;
}
else if(result.height == 1136)
{
// iPhone 4 inches Screen
scaleY=1.18333f;
}
}
}
Then multiply the scaleY value with your "y" position of the views.
UIImageView *imgLogo=[[UIImageView alloc]initWithFrame:CGRectMake(75, 10*scaleY, 162, 57)];
imgLogo.image=[UIImage imageNamed:#"Logo-01.png"];
[self.view addSubview:imgLogo];
UIImageView *imgBuilding=[[UIImageView alloc]initWithFrame:CGRectMake(0, 320*scaleY, 320, 140 )];
imgBuilding.image=[UIImage imageNamed:#"image-02.png"];
[self.view addSubview:imgBuilding];
You can use this "scaleY" value (as shown above) throughout your code to multiply it with your y position of the corresponding views. Since iPhone Family Devices are different in heights so we can only change their "y" position.
Hope this helps.
just try with autoresizingmask like this.
UIImageView *imgLogo=[[UIImageView alloc]initWithFrame:CGRectMake(75, 10, 162, 57)];
imgLogo.image=[UIImage imageNamed:#"Logo-01.png"];
[imgLogo setAutoresizingMask:(UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin)];
[self.view addSubview:imgLogo];
This is a quick fix to your issue but i would suggest to use Auto layout.
if ([[UIScreen mainScreen] bounds].size.height == 568)
{
imgLogo=[[UIImageView alloc]initWithFrame:CGRectMake(75, 10, 162, 57)];
imgLogo.image=[UIImage imageNamed:#"Logo-01.png"];
[self.view addSubview:imgLogo];
imgBuilding=[[UIImageView alloc]initWithFrame:CGRectMake(0, 320, 420,140 )];
imgBuilding.image=[UIImage imageNamed:#"image-02.png"];
[self.view addSubview:imgBuilding];
}
else
{
imgLogo=[[UIImageView alloc]initWithFrame:CGRectMake(75, 10, 162, 57)];
imgLogo.image=[UIImage imageNamed:#"Logo-01.png"];
[self.view addSubview:imgLogo];
imgBuilding=[[UIImageView alloc]initWithFrame:CGRectMake(0, 320, 320,140 )];
imgBuilding.image=[UIImage imageNamed:#"image-02.png"];
[self.view addSubview:imgBuilding];
}
Hope this will help you out.
I'm a beginner with iOS, so i'm just not sure what to research here. I have a UIScrollView with a few square subViews added. How can i make the subviews smaller as they scroll off screen and bigger as they approach the center of the screen?
#import "HorizontalScrollMenuViewController.h"
#import <UIKit/UIKit.h>
#define SUBVIEW_WIDTH_HEIGHT 280
#interface HorizontalScrollMenuViewController : UIViewController
#property (nonatomic, strong) IBOutlet UIScrollView *scrollView;
#end
#implementation HorizontalScrollMenuViewController
-(void)viewDidLoad{
[super viewDidLoad];
NSArray *colors = [NSArray arrayWithObjects:[UIColor greenColor],[UIColor redColor],[UIColor orangeColor],[UIColor blueColor],nil ];
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
CGFloat originX = (screenWidth - SUBVIEW_WIDTH_HEIGHT)/2.0; // get margin to left and right of subview
CGFloat originY = ((screenHeight - SUBVIEW_WIDTH_HEIGHT)/2);
// add subviews of all activities
for (int i = 0; i < colors.count; i++){
CGRect frame = CGRectMake(0,0,SUBVIEW_WIDTH_HEIGHT,SUBVIEW_WIDTH_HEIGHT);
frame.origin.x = self.scrollView.frame.size.width * i + originX;
frame.origin.y = originY;
UIView *subView = [[UIView alloc] initWithFrame:frame];
[UIView setAnimationBeginsFromCurrentState: YES];
subView.layer.cornerRadius = 15;
subView.layer.masksToBounds = YES;
subView.backgroundColor = [colors objectAtIndex:i];
[self.scrollView addSubview:subView];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height);
}
#end
Here you can find a fully working example of what you're trying to accomplish. It only has
one subview because it's just to give you an idea of how can you accomplish it. Also, this example was tested on an iPad (iOS7) simulator.
The *.h file
#import <UIKit/UIKit.h>
// Remember to declare ourselves as the scroll view delegate
#interface TSViewController : UIViewController <UIScrollViewDelegate>
#property (nonatomic, strong) UIView *squareView;
#end
The *.m file
#import "TSViewController.h"
#implementation TSViewController
#synthesize squareView = _squareView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Create and configure the scroll view (light gray)
UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(100, 100, 500, 500)];
CGRect contentSize = myScrollView.frame;
contentSize.size.height = contentSize.size.height + 400;
myScrollView.contentSize = contentSize.size;
myScrollView.userInteractionEnabled = YES;
// give the scroll view a gray color so it's easily identifiable
myScrollView.backgroundColor = [UIColor lightGrayColor];
// remember to set yourself as the delegate of the scroll view
myScrollView.delegate = self;
[self.view addSubview:myScrollView];
// Create and configure the square view (blue)
self.squareView = [[UIView alloc] initWithFrame:CGRectMake(200, 400, 60, 60)];
self.squareView.backgroundColor = [UIColor blueColor];
[myScrollView addSubview:self.squareView];
}
// Here is where all the work happens
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
// Get the difference between the contentOffset y position and the squareView y position
CGFloat y = self.squareView.frame.origin.y - scrollView.contentOffset.y;
// If the square has gone out of view, return
if (y <= 0) {
return;
}
// Modify the squareView's frame depending on it's current position
CGRect squareViewFrame = self.squareView.frame;
squareViewFrame.size.height = y + 5;
squareViewFrame.size.width = y + 5;
squareViewFrame.origin.x = (scrollView.contentSize.width - squareViewFrame.size.width) / 2.0;
self.squareView.frame = squareViewFrame;
}
#end
And here is a little explanation of what is going on:
A UIScrollView has several properties that allow you to configure it correctly. For example it has a frame (gray) which is inherited from UIView; with this property you specify the visible size of the scroll view. It also has the contentSize (red) which specifies the total size of the scroll view; in the image it's showed as the red area but this is only for illustration purposes as it will not be visible in the program. Imagine the scroll view's frame as the window that let's you see only a part of the bigger content the scroll view has.
When the user starts scrolling a gap appears between the top part of the contentSize and the top part of the frame. This gap is known as the contentOffset
Here is the reference to UIScrollView
Here is the reference to UIScrollViewDelegate
Hope this helps!
Assuming that you have the scrollView inside self.view, you can implement scrollViewDidScroll: in the scroll view delegate to find when it is scrolled.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
for (UIView *view in self.scrollView.subviews) {
CGRect frame = [view convertRect:view.frame toView:self.view]; // Contains the frame of view with respect to self.view
}
}
You can them use the frame to resize subviews as you want.
The answer starts with analyzing the UIScrollView Class Reference and it's delegate. In the delegate documentation see the responding to scrolling and dragging section. You should also review the sample code for each. You can create outlets to your subviews and change the subview properties within a uiview animation. These references will give you a good foundation in understanding where you can build the call to animate the subviews.
Here is a link to animating subviews. Additional examples can be found by Googling "uiview subview animation" (without the quotes). If you run into any major issues read the header files first and post some sample code for additional (more precise) help.
Other reference:
UIKit ScrollViews
I want to display an UITextView on centre, dynamically both for 3.5 inch display and 4 inch display. but it has top and bottom 'padding' 20 pts.
So I create this code to measure UIView's height:
CGFloat screenHeight = _screen.frame.size.height;
float scrollHeight = screenHeight - 40;
then I add this line to set the screen :
_screen.frame = CGRectMake(20, 40, 320, scrollHeight);
but I see no difference on my 4 inch iPod touch... what did I do wrong?
UPDATE :
here's my .h file :
#property (strong, nonatomic) IBOutlet UIView *screen;
and here's my .m file :
- (void)viewDidLoad
{
[super viewDidLoad];
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
_screen.frame = CGRectMake(20, 40, 320, screenHeight);
}
You can get the screen informations for sure like this:
screenWidth = [UIScreen mainScreen].bounds.size.width;
screenHeight = [UIScreen mainScreen].bounds.size.height;
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
You can calculate with these dynamically every height or width you want, and your views will got the right frame.
EDIT:
- (void)viewDidLoad
{
[super viewDidLoad];
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
_screen.frame = CGRectMake(20, 40, 320, screenHeight);
}
this will make you the _screen will start at 40 and it will hang out at the bottom 40 too.
if you want to make a centered view make it like this:
_screen.frame = CGRectMake(20, 40, screenWidth-40, screenHeight-80);
and don't forget to add the screen to the superview like:
[self.view addSubview:_screen];
I hope it helps.
And one more tip: You can see your _screen frame, if you set _screen.backgroundcolor = [UIColor orangeColor]; for testing !