【问题标题】:Getting Screen width and height [duplicate]获取屏幕宽度和高度
【发布时间】:2013-09-04 13:05:24
【问题描述】:

我正在使用 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];
}

提前致谢。

【问题讨论】:

  • 在问非常明显的问题之前请使用搜索功能

标签: objective-c


【解决方案1】:
float height = [[UIScreen mainScreen] bounds].size.height

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

【讨论】:

  • 它也适用于 Cordova 插件。
【解决方案2】:

屏幕高度/宽度可以从以下位置获取:

[[UIScreen mainScreen] bounds].size.height

[[UIScreen mainScreen] bounds].size.width

【讨论】:

    猜你喜欢
    • 2010-11-25
    • 2011-09-25
    • 2011-06-12
    • 2012-11-01
    • 2021-02-07
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多