0

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?

TomH
  • 2,950
  • 2
  • 26
  • 49
arc4randall
  • 3,275
  • 3
  • 27
  • 40
  • change the code `CGRectMake(0,screenHeight - (screenHeight / 2), screenWidth, screenHeight / 2);` – nagarajan Feb 17 '15 at 19:30
  • iOS relies on the size / bounds of the Launch Image in order to get the bounds of the screen. What value is your launch image? Could you tell us what value screenRect shows? http://stackoverflow.com/questions/21668497/uiscreen-mainscreen-bounds-returning-wrong-size – gran_profaci Feb 17 '15 at 19:31
  • Your CGRectMake seems to be correct since I believe you're looking to have a box of (screenwidth / 2) at the same distance away from the bottom. – gran_profaci Feb 17 '15 at 19:32

2 Answers2

0

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);
}
Claudio Castro
  • 1,541
  • 12
  • 28
  • in that case I would just do CGRectMake(0, screenHeight/2, screenWidth, screenHeight /2); It was a matter of not constraining to the bottom, not an issue with the initial y coordinate or the height. The original square is the desired size – arc4randall Feb 17 '15 at 21:06
0

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)];
arc4randall
  • 3,275
  • 3
  • 27
  • 40