gpt4 book ai didi

iphone - 如何获得 UIView 的正确大小?

转载 作者:行者123 更新时间:2023-11-28 22:41:54 25 4
gpt4 key购买 nike

问题 1:

如何获得 UIView 的正确大小?

我正在创建一个 CGRect 以使用平铺图层显示一些图像。当我创建 CGRect 时,我基本上需要它与我的 UIView 的大小完全相同。事实证明这很难..当我 NSLog() 输出我的 mainView.bounds.size.width 或我的 mainView.frame.size.width 时,它们在横向时总是错误的!他们总是像纵向一样注销这些值,即使我可以看到实际的视野更宽。颠倒它们也是错误的。将宽度设置为高度不够好,在横向时反之亦然,我需要正确的值。

我能够让它看起来正确的唯一方法是在横向时手动输入 1024 宽度,但这并不总是有效,因为:

问题 2:

检查设备是否横向的正确方法是什么?

我一直在用

if([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft || [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight)

但这并不总是有效。如果我在启动时将我的设备保持在横向模式,这是正确的,但如果我让它横向,然后将 iPad 平放,让仪表板保持横向,然后启动它,然后横向启动显示,但该代码认为它是纵向的。该代码也不适用于 iPad 模拟器。

编辑

出于某种原因,当我决定添加对横向方向的支持时,仅仅检查目标摘要页面中的横向方向是不够的,我必须实际对我的 TabBarController 进行子类化并实际告诉它与

一起旋转
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return YES;
}

我不应该这样做......对吧?如果我创建一个这样的空项目,它就不需要它。我不知道为什么。

最佳答案

问题一:是的,没错。 :)

问题二:我刚回到家,检查了自己的代码。我使用:

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

检查当前界面的方向。然后,您可以使用类似的东西:

if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) {
// Do something
} else if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation)){
// Do something else
}

但是如果您正确处理旋转事件,您真的不需要这样做。

当我需要根据方向调整代码中的 UI 元素位置时,这是我处理旋转的典型方法:

#pragma mark - View rotation methods

// Maintain pre-iOS 6 support:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}

// Make sure that our subviews get moved on launch:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
[self moveSubviewsToOrientation:orientation duration:0.0];
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];

[self moveSubviewsToOrientation:toInterfaceOrientation duration:duration];
}

// Animate the movements
- (void)moveSubviewsToOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration
{
[UIView animateWithDuration:duration
animations:^{
[self.tableView reloadData];
if (UIInterfaceOrientationIsPortrait(orientation))
{
[self moveSubviewsToPortrait];
}
else
{
[self moveSubviewsToLandscape];
}
}
completion:NULL];
}

- (void)moveSubviewsToPortrait
{
// Set the frames/etc for portrait presentation
self.logoImageView.frame = CGRectMake(229.0, 21.0, 309.0, 55.0);
}

- (void)moveSubviewsToLandscape
{
// Set the frames/etc for landscape presentation
self.logoImageView.frame = CGRectMake(88.0, 21.0, 309.0, 55.0);
}

我还在 viewWillAppear 中放置了 moveSubviewsToOrientation 让它旋转

关于iphone - 如何获得 UIView 的正确大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14288152/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com