I am attempting to make a UIView that is constrained to the bottom of the container view without actually doing auto layout an constraints. Here is my code:
-(void)viewDidLoad
{
[super viewDidLoad];
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
self.graphView.frame = CGRectMake(0,screenHeight - (screenWidth / 2), screenWidth, screenWidth / 2);
}
I was hoping this would create a subview that would appear pinned to the bottom of the screen and be the width of the screen and half that tall. However, when the view loads, there is some space between my graphView and the bottom of the screen. Any clue why this is?
I think you are using screenWidth when you need use screenHeight
The correct code:
-(void)viewDidLoad
{
[super viewDidLoad];
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
self.graphView.frame = CGRectMake(0,screenHeight - (screenHeight / 2), screenWidth, screenHeight / 2);
}
The solution: Turn off auto layout and any other IB toggles that have an effect on where the view is being placed. The original code is correct for the rectangle I wanted, it was just being moved by auto layout. Also it's better to set up the graph container like this:
self.graphContainer = [[UIView alloc] initWithFrame:CGRectMake(0,screenHeight - (screenHeight / 2), screenWidth, screenHeight / 2)];
Related
I have a UIScrollView in my application and i want to basically add a button on the final page. Right now i add the button in story board under the Scroll View and set the X parameter of the button to 1050 which is (screenWidth*3 + 90) with screenWidth as 320 (iPhone5). So as you can see it works on iPhone5 but doesn't work on larger screens 6/6Plus. I tried to readjust the button programatically but it didn't work.
-(void)viewDidAppear:(BOOL)animated
{
...
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
[self.tutorialScrollView addSubview:page1];
[self.tutorialScrollView addSubview:page2];
[self.tutorialScrollView addSubview:page3];
[self.tutorialScrollView addSubview:page4];
[self.tutorialScrollView setContentSize:CGSizeMake(screenWidth*4, screenHeight)];
//readjust button
CGRect btFrame = self.tutorialFinish.frame; //self.tutorialFinish is a UIButton
btFrame.origin.x = (90 + screenWidth * 3);
btFrame.origin.y = 492/568 * screenHeight;
self.tutorialFinish.frame = btFrame;
}
Try to adjust center of the btn:
self.tutorialFinish.center = CGPointMake(90 + screenWidth * 3 + self.tutorialFinish.frame.size.width/2,
492/568 * screenHeight + self.tutorialFinish.frame.size.height/2);;
You should wrap your UIScrollView with some other UIView to know the screen size. You can later pass it in automatic way to your scroll view as lets say layoutView. In code I mean, I don't know how to do such a thing in Storyboards but I assume it is possible.
From then you should be able to adjust your views to any existing or future screen size.
CGRect buttonFrame = CGRectMake(screenwidth * (page-1)+ (screenwidth
-self.button.frame.size.width)/2, y position, button width, button height);
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];
I have an object of type UINavigationBar that I'm trying to reposition when a UITextField object triggers it's Editing Did Begin event. However I'm running into a strange error where when the following code is called:
- (IBAction)prepForTabBarView:(id)sender{
NSLog(#"Test");
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
CGFloat navBarPosition = screenHeight - 350 - 64;
[self.navBar setFrame:CGRectMake(0, navBarPosition, screenWidth , 44)];
}
The first time the event is called the navBar doesn't move however the second time it's called the navBar moves. However both times the NSLog appears. What's going on here? And how can I fix it?
I'm getting in a bit of a muddle trying to draw some paintcode graphics code in the middle of the screen horizontally.
I'm really not sure if scale or landscape orientation affects this.
I've created a UIView subclass and addSubview in viewDidLoad like so...
MenuTitleView *fancyView = [[MenuTitleView alloc] initWithFrame:self.view.bounds];
[[self view] addSubview:fancyView];
Rather than the size PaintCode gave me...
CGRect textRect = CGRectMake(0, 0, 418, 129);
I'm trying to determine the screen / view width and the size of the canvas width.
I've tried various things without success.
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
CGFloat w = screenRect.size.width;
CGFloat width = 500;
CGFloat height = 200;
CGFloat x = (w - width) * 0.5f;
CGFloat y = 20;
CGRect textRect = CGRectMake(x, y, width, height);
Scale is not an issue, because Quartz and UIKit now work in 'points' rather than in pixels, so that's already accounted for. However orientation is not, [UIScreen mainScreen] will return the same rect either for applicationFrame or bounds etc, it's oblivious to orientation.
I like to put a solution in a category on UIView or UIViewController, because you'll reuse it plenty. something like this...
-(CGRect )workingCanvas{
CGRect screenRect = [UIScreen mainScreen].bounds;
BOOL landscape = UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]);
CGRect result = CGRectZero;
CGFloat lesserDimension = (screenRect.size.width < screenRect.size.height) ? screenRect.size.width : screenRect.size.height;
CGFloat greaterDimension = (screenRect.size.width > screenRect.size.height) ? screenRect.size.width : screenRect.size.height;
result.size.width = (landscape) ? greaterDimension : lesserDimension;
result.size.height = (landscape) ? lesserDimension : greaterDimension;
if ([[UIApplication sharedApplication]isStatusBarVisible]){
result.size.height -= [[UIApplication sharedApplication]statusBarFrame].size.height;
}
return result;
}
if you want to position your view just in the middle of the screen try this
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
CGFloat width = 500;
CGFloat height = 200;
CGFloat x = CGRectGetMidX(screenRect);
CGFloat y = 20;
CGRect textRect = CGRectMake(x, y, width, height);
already tested is working
Good Luck!!
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 !