【发布时间】:2014-04-01 16:31:00
【问题描述】:
我有下面的代码来添加一些UIBarButtons 到我的项目中。这适用于以下情况:
全部在 iOS 7.1 上
- 模拟器 iPhone Retina 3.5 英寸;
- 模拟器 iPhone 视网膜 4 英寸;
- 模拟器 iPhone Retina 4 英寸(64 位)
- iPhone 4
- iPhone 4s
不过,我没有要测试的 iPhone 5 设备。
它不适用于新的 iPhone 5s。有什么不同?
代码如下:
-(void)setupNavigationBar
{
self.saveSearchButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.saveSearchButton setImage:[UIImage imageNamed:@"Save Search"] forState:UIControlStateNormal];
[self.saveSearchButton setImage:[UIImage imageNamed:@"Save Search Active"] forState:UIControlStateHighlighted|UIControlStateSelected];
[self.saveSearchButton addTarget:self action:@selector(saveSearchButtonpressed)forControlEvents:UIControlEventTouchUpInside];
[self.saveSearchButton setFrame:CGRectMake(0, 0, 23, 31)];
self.changeLayutButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.changeLayutButton setImage:[UIImage imageNamed:@"View List"] forState:UIControlStateNormal];
[self.changeLayutButton setImage:[UIImage imageNamed:@"View Grid"] forState:UIControlStateHighlighted|UIControlStateSelected];
[self.changeLayutButton addTarget:self action:@selector(changeViewLayoutButtonPressed)forControlEvents:UIControlEventTouchUpInside];
[self.changeLayutButton setFrame:CGRectMake(0, 0, 23, 31)];
self.sortByButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.sortByButton setImage:[UIImage imageNamed:@"Sort By"] forState:UIControlStateNormal];
[self.sortByButton setImage:[UIImage imageNamed:@"Sort By Active"] forState:UIControlStateHighlighted|UIControlStateSelected];
[self.sortByButton addTarget:self action:@selector(sortByButtonPressed)forControlEvents:UIControlEventTouchUpInside];
[self.sortByButton setFrame:CGRectMake(0, 0, 23, 31)];
UIBarButtonItem *fixedItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
fixedItem.width = 20;
UIBarButtonItem *saveSearchButton = [[UIBarButtonItem alloc] initWithCustomView:self.saveSearchButton];
UIBarButtonItem *changeViewButton = [[UIBarButtonItem alloc] initWithCustomView:self.changeLayutButton];
UIBarButtonItem *sortByButton = [[UIBarButtonItem alloc] initWithCustomView:self.sortByButton];
NSArray *barbuttonitems = @[sortByButton, fixedItem, changeViewButton, fixedItem,saveSearchButton];
self.navigationItem.rightBarButtonItems = barbuttonitems;
self.lastLayoutUsed = [[NSUserDefaults standardUserDefaults]objectForKey:@"LastLayoutUsed"];
if ([self.lastLayoutUsed isEqualToString:@"GridLayout"]){
self.changeLayutButton.selected = YES;
[NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(highlightButton:) userInfo:self.changeLayutButton repeats:NO];
}
}
我已经单步执行了代码,所有属性都不是 nil 并且具有有效值。我也检查了所有图像都是正确的一面。
这将如何在模拟器上的每台设备上运行,而不是在实际的 iPhone 5S 上运行?
另外,由于这两款手机(iPhone 4S 和 5S)的屏幕宽度和 iOS 版本完全相同,我真的很困惑吗?
按钮根本不显示。没有编译器警告,也没有控制台错误。
更新
在 iPhone 5 上测试了上面的代码,它工作得很好。这让人相信它一定与 iPhone 5S 的 64 位有关?
更新 2
从方法中删除所有代码并将其更改为一个非常简单的按钮,如下所示:
self.saveSearchButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.saveSearchButton setTitle:@"Save" forState:UIControlStateNormal];
[self.saveSearchButton setTintColor:[UIColor blueColor]];
UIBarButtonItem *saveSearchButton = [[UIBarButtonItem alloc] initWithCustomView:self.saveSearchButton];
self.navigationItem.rightBarButtonItem = saveSearchButton;
这现在不适用于任何设备或模拟器。
我在这里做错了什么?
更新 3 - 解决方案!
是的,所以这只是一件简单的事情引起了所有这些大惊小怪。我将我的 UIButtons 声明为 weak 而不是 strong - 我的印象是 UI 元素需要很弱,因为它们经常离开视图?
我在 cmets 部分的帮助下得出了这个答案。
这也没有解释为什么它在声明为weak 时可以在 iPhone 4S 和 iPhone 5 上运行。它也适用于声明为 weak 的 64 位模拟器
这是否意味着将使用 64 位模拟器作为指导,并且必须在设备上进行实际测试,因为在 UIKit 测试时模拟器似乎不准确?
我很想了解更多关于这方面的信息。
【问题讨论】:
-
请解释其中哪一部分不起作用。它是否显示任何内容,任何错误?..
-
对不起,应该已经很清楚了。按钮根本不显示。没有编译器警告也没有控制台警告。
-
“这将如何在模拟器上的每台设备上运行,而不是在实际的 iPhone 5S 上运行?”无论如何,模拟器都不是完美的。
-
我开始越来越意识到这一点。现在将所有 UI 测试转移到实际设备上。
-
在黑暗中拍摄,但是您是否尝试过在需要的地方使用双精度/浮点数,例如此处 CGRectMake(0, 0, 23, 31) -> CGRectMake(0.0, 0.0, 23.0, 31.0) ?
标签: ios iphone objective-c