我正在尝试以编程方式确定我的应用程序的当前高度和宽度.我用这个:
CGRect screenRect = [[UIScreen mainScreen] bounds];
Run Code Online (Sandbox Code Playgroud)
但无论设备是纵向还是横向,这都会产生320的宽度和480的高度.如何确定主屏幕的当前宽度和高度(即取决于设备方向)?
嗨,我想知道是否有办法以编程方式获得宽度.
我正在寻找足以容纳iphone 3gs,iphone 4,ipad的东西.此外,宽度应根据设备是纵向还是横向(对于ipad)而改变.
谁知道怎么做?我一直在寻找...谢谢!
我正在开发一个带有最新SDK的iOS应用程序.
我已将Landscape Right orientation设置为可用的唯一方向,我对viewController视图有疑问.
这是我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(deviceOrientationDidChange:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != AVCaptureVideoOrientationLandscapeRight);
}
- (void)deviceOrientationDidChange:(NSNotification*)notification
{
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (orientation == AVCaptureVideoOrientationLandscapeLeft)
NSLog(@"Orientation: Landscape Left");
else if (orientation == AVCaptureVideoOrientationLandscapeRight)
NSLog(@"Orientation: Landscape Right");
NSLog(@"FRAME: %@", NSStringFromCGRect(self.view.frame));
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的日志:
Orientation: Landscape Right
FRAME: {{0, 0}, {320, 568}}
Orientation: Landscape Right
FRAME: {{0, 0}, {320, 568}}
Run Code Online (Sandbox Code Playgroud)
我的问题是{{0, 0}, {320, 568}}
:
为什么我会得到这些价值?
我认为正确的价值观是 …
我正在开发一个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)
为什么这样,我该怎么做?
我正在尝试使用我的应用程序解决一个非常令人沮丧的问题 - 在研究它时我可能在iOS中发现了一个错误:
旋转iPad时,视图会旋转,但它的框架不会改变.我正在运行一个计时器,每秒打印出我的视图的宽度和高度,虽然我可以看到视图在视觉上旋转,但框架仍然是"768x1024",无论如何.
来自Apple的文档:
当用户界面旋转时,将调整窗口大小以匹配新方向.窗口调整其根视图控制器的框架以匹配新大小,并且此大小依次沿视图层次结构传播到其他视图.
这正是未发生的事情,它给我带来了各种各样的麻烦.有谁知道如何修复旋转,以便框架改变?
重现步骤:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.rootViewController = [[UIViewController alloc] init]; NSLog(@"%fx%f", self.window.rootViewController.view.frame.size.width, self.window.rootViewController.view.frame.size.height); // Add a label to confirm orientation UILabel *orientationLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,10,100,30)]; orientationLabel.text = @"Top Left"; [self.window.rootViewController.view addSubview: orientationLabel]; [NSTimer scheduledTimerWithTimeInterval:.3 target:self selector:@selector(testScale) userInfo:nil repeats:YES]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; }
- (void) testScale { NSLog(@"%fx%f", self.window.rootViewController.view.frame.size.width, self.window.rootViewController.view.frame.size.height); }
ios ×4
objective-c ×3
cocoa-touch ×2
ipad ×2
iphone ×2
cocoa ×1
frame ×1
ios6 ×1
rotation ×1
uiview ×1