Rob*_*egg 7 iphone objective-c ios
我有以下代码为UIBarButton我的项目添加一些s.这适用于以下情况:
全部在iOS 7.1上
我没有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];
}
}
Run Code Online (Sandbox Code Playgroud)
我已经完成了代码,并且所有属性都不是nil并且具有有效值.我已经检查过所有图像也是正确的一面.
如何在模拟器上的每个设备上工作,而不是真正的iPhone 5S?
此外,由于这两款手机(iPhone 4S和5S)拥有完全相同的屏幕宽度和iOS版本,我真的很困惑?
按钮根本不显示.没有编译器警告和没有控制台错误.
UPDATE
在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;
Run Code Online (Sandbox Code Playgroud)
现在,这不适用于任何设备或模拟器.
我在这做错了什么?
更新3 - 解决方案!
是的,所以这是一件简单的事情,引起了所有这些大惊小怪.我宣称我的UIButton weak不是strong- 我的印象是UI元素需要很弱,因为它们常常偏离视线?
我在评论部分的帮助下找到了这个答案.
这也无法解释为什么它在声明为iPhone 4S和iPhone 5时有效weak.它也适用于声明为64位模拟器weak
这是否意味着64位模拟器将用作指南,并且必须在设备上进行实际测试,因为在UIKit测试时模拟器似乎不准确?
我想知道更多关于此事的信息.
问题是[UIButton buttonWithType:UIButtonTypeCustom]不会留下Apple Docs中所述的对象实例.UIBarButton期待一些实例.https://developer.apple.com/library/ios/documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html
AppleDocs中所述的解决方案是使用alloc + init构造来创建一个按钮.
buttonWithType:
"此方法是一个方便构造函数,用于创建具有特定配置的按钮对象.如果您继承UIButton,则此方法不返回子类的实例.如果要创建特定子类的实例,则必须分配/初始化按钮直."
| 归档时间: |
|
| 查看次数: |
1076 次 |
| 最近记录: |