-1

Im creating a new subview in XCode using CGRectmake. This I can do fine. But I would like to make the size of the new view 10 pixels less than the screen width. I normally work with Java and would use screen.width-10 to do this. But with ObjC in Xcode I'm not sure how to and couldn't find the info i needed. This what I have so far it creates a box on a button tap but i would like to change the width from 100 to screen width - 10.

//Info Button
-(IBAction) buttonTapped:(id)sender {
    // create a new UIView
    UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(10,10,100,100)];

    // do something, e.g. set the background color to red
    newView.backgroundColor = [UIColor redColor];

    // add the new view as a subview to an existing one (e.g. self.view)
    [self.view addSubview:newView];

    // release the newView as -addSubview: will retain it
    [newView release];
}

Thanks in advance.

dandan78
  • 13,328
  • 13
  • 64
  • 78
MrHolroyd
  • 51
  • 1
  • 1
  • 7

2 Answers2

11
float height = [[UIScreen mainScreen] bounds].size.height

float width = [[UIScreen mainScreen] bounds].size.width
Larme
  • 24,190
  • 6
  • 51
  • 81
5

The screen height / width can be retrieved from:

[[UIScreen mainScreen] bounds].size.height

[[UIScreen mainScreen] bounds].size.width
Simon McLoughlin
  • 8,293
  • 5
  • 32
  • 56