获取屏幕宽度和高度

时间:2013-09-04 13:05:25

标签: objective-c

我使用CGRectmake在XCode中创建一个新的子视图。我可以做得很好。但我想使新视图的大小比屏幕宽度小10个像素。我通常使用Java并使用screen.width-10来执行此操作。但是在Xcode中使用ObjC我不知道如何找到我需要的信息。这就是我到目前为止它在按钮水龙头上创建一个框但我想将宽度从100更改为屏幕宽度--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];
}

提前致谢。

2 个答案:

答案 0 :(得分:10)

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

float width = [[UIScreen mainScreen] bounds].size.width

答案 1 :(得分:4)

可以从以下位置检索屏幕高度/宽度:

[[UIScreen mainScreen] bounds].size.height

[[UIScreen mainScreen] bounds].size.width
相关问题