我正在尝试以编程方式确定我的应用程序的当前高度和宽度.我用这个:
CGRect screenRect = [[UIScreen mainScreen] bounds];
Run Code Online (Sandbox Code Playgroud)
但无论设备是纵向还是横向,这都会产生320的宽度和480的高度.如何确定主屏幕的当前宽度和高度(即取决于设备方向)?
在我的iPhone OS应用程序中,我希望(需要)观察设备方向的变化,以便重新排列屏幕的某些部分.我使用的方法是用来CGRect frame = [UIScreen mainScreen].applicationFrame
获取屏幕大小,并从那里计算其他控件的大小和/或位置(我也尝试过self.view.frame
).
所有测试都是在Portrait模式下完成的,所以我可以专注于编写主要功能,然后再对Landscape进行一些调整.这里输入问题:-(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
我在继续之前添加了一些日志来检查大小,但显然宽度和高度的值是"错误的"(我说"错误",因为乍一看这些值对我来说没有意义) .
这是一些日志记录的输出:
Landscape中"w"和"h"的值似乎与我相反 - 我期待w = 480和h = 300.
我究竟做错了什么?我用来调试的代码如下.
-(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
CGRect frame = [UIScreen mainScreen].applicationFrame;
CGSize size = frame.size;
NSLog(@"%@", [NSString stringWithFormat:@"Rotation: %s [w=%f, h=%f]",
UIInterfaceOrientationIsPortrait(self.interfaceOrientation) ? "Portrait" : "Landscape",
size.width, size.height]);
}
Run Code Online (Sandbox Code Playgroud) 考虑到各种iOS设备和方向,获取当前屏幕分辨率的最简单方法是什么,包括高度和宽度?
我正在开发一个iPad项目,目前它是在Landscape视图上.我试着这样做:
self.view.frame.size.height
Run Code Online (Sandbox Code Playgroud)
为什么这总是返回960?虽然在景观中,视图本身的高度尺寸应为768对吗?
我想要做的是allocInitWithFrame位于底部的UIToolbar.该UIToolBar有50的高度,所以这里是我所做的这些失败:
self.bottom_bar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.frame.size.height-50, self.view.frame.size.width, 50)];
Run Code Online (Sandbox Code Playgroud)
为什么这样,我该怎么做?