-2

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
  • 12,893
  • 12
  • 63
  • 75
MrHolroyd
  • 41
  • 1
  • 1
  • 7

2 Answers2

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

float width = [[UIScreen mainScreen] bounds].size.width
Larme
  • 22,070
  • 5
  • 50
  • 76
5

The screen height / width can be retrieved from:

[[UIScreen mainScreen] bounds].size.height

[[UIScreen mainScreen] bounds].size.width
Simon McLoughlin
  • 7,979
  • 5
  • 34
  • 55