微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ios – 为什么这个代码适用于iPhone 5,4和4但不适用于5s?

我有以下代码,为我的项目添加一些UIBarButtons.这适用于以下情况:

全部在iOS 7.1上

>模拟器iPhone Retina 3.5英寸;
>模拟器iPhone Retina 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,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,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,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,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版本,我真的很困惑?

按钮根本不显示.没有编译器警告和没有控制台错误.

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;

现在,这不适用于任何设备或模拟器.

在这做错了什么?

更新3 – 解决方案!

是的,所以这是一件简单的事情,引起了所有这些大惊小怪.我宣称我的UIButtons很弱而且不强 – 我的印象是UI元素需要很弱,因为它们常常会脱离视线?

我在评论部分的帮助下找到了这个答案.

这也无法解释为什么它在声称为弱的情况下适用于iPhone 4S和iPhone 5.它也适用于声称为弱的64位模拟器

这是否意味着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:

“This method is a convenience constructor for creating button objects with specific configurations. If you subclass UIButton,this method does not return an instance of your subclass. If you want to create an instance of a specific subclass,you must alloc/init the button directly.”

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐