这是我第一次以编程方式添加UI元素。该应用程序崩溃并出现错误:

2014-01-14 18:39:50.080 Convention[27580:70b] -[__NSCFConstantString alignmentRectInsets]: unrecognized selector sent to instance 0x10c028
2014-01-14 18:40:00.792 Convention[27580:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString alignmentRectInsets]: unrecognized selector sent to instance 0x10c028'
*** First throw call stack:
(
    0   CoreFoundation                      0x023f55e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x01fe48b6 objc_exception_throw + 44
    2   CoreFoundation                      0x02492903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
    3   CoreFoundation                      0x023e590b ___forwarding___ + 1019
    4   CoreFoundation                      0x0242246e __forwarding_prep_1___ + 14
    5   UIKit                               0x00fe2305 -[UIButton alignmentRectInsets] + 217
    6   UIKit                               0x013b605b -[UIView(AdditionalLayoutSupport) alignmentRectForFrame:] + 52
    7   UIKit                               0x00fdf7f0 -[UIButton contentRectForBounds:] + 104
    8   UIKit                               0x00fe2448 -[UIButton layoutSubviews] + 100
    9   UIKit                               0x00dbd267 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
    10  libobjc.A.dylib                     0x01ff681f -[NSObject performSelector:withObject:] + 70
    11  QuartzCore                          0x0029f2ea -[CALayer layoutSublayers] + 148
    12  QuartzCore                          0x002930d4 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
    13  QuartzCore                          0x002a1715 -[CALayer(CALayerPrivate) layoutBelowIfNeeded] + 43
    14  UIKit                               0x00dafc76 -[UIView(Hierarchy) layoutBelowIfNeeded] + 595
    15  UIKit                               0x00fdb18c -[UIButton setFrame:] + 182
    16  Convention                          0x0006c020 -[CIFinalCustomerInfoViewController viewDidLoad] + 1760
    17  UIKit                               0x00e66318 -[UIViewController loadViewIfRequired] + 696

执行cancelButton.frame = CGRectMake(62.0, currentY+8.0, 162.0, 56.0);时发生错误。以下是代码:
- (void)viewDidLoad {
    [super viewDidLoad];
    BOOL contactb4ShippingConfig = ([ShowConfigurations instance].contactBeforeShipping);
    originalBounds = self.view.bounds;
    CGFloat currentY = 404.0;
    if (contactb4ShippingConfig) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(62.0, currentY, 300.0, 35.0)];
        label.font = [UIFont fontWithName:@"Futura Medium Italic" size:27.0f];
        label.textColor = [UIColor whiteColor];
        label.text = @"Contact Before Shipping";
        [self.view addSubview:label];
        [self.view addSubview:contactBeforeShippingCB];
        currentY = currentY + 35.0;
    }
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelButton addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchDown];
    [cancelButton setBackgroundImage:@"cart-cancelout.png" forState:UIControlStateNormal];
    [cancelButton setBackgroundImage:@"cart-cancelin.png" forState:UIControlStateHighlighted];
    cancelButton.frame = CGRectMake(62.0, currentY+8.0, 162.0, 56.0);
    UIButton *submitButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [submitButton addTarget:self action:@selector(submit:) forControlEvents:UIControlEventTouchDown];
    [submitButton setBackgroundImage:@"submitorderout.png" forState:UIControlStateNormal];
    [submitButton setBackgroundImage:@"submitorderin.png" forState:UIControlStateSelected];
    submitButton.frame = CGRectMake(62.0, cancelButton.frame.origin.y, 260.0, 56.0);
    currentY = cancelButton.frame.origin.y;
    [self.view addSubview:cancelButton];
    [self.view sizeToFit];
    [self.view layoutIfNeeded];
}

最佳答案

问题是这些行:

[cancelButton setBackgroundImage:@"cart-cancelout.png" forState:UIControlStateNormal];
[cancelButton setBackgroundImage:@"cart-cancelin.png" forState:UIControlStateHighlighted];

评论他们,一切都会好起来的。这表明我们未能找到有问题的图像。

稍后编辑:好吧,我很粗心,恐怕;我纯粹是通过尝试和错误来找出问题线,但是我完全没有正确地注意到它们的问题所在!正如您正确地说的,这是字符串,而不是图像。的确,我们应该从原始错误消息中知道这一点:“-[__NSCFConstantString alignmentRectInsets]”表示恰好在期望其他类的地方提供了一个字符串,因此我们要做的就是在代码中寻找一些流氓字符串。

(您的代码还有其他一些问题,但不是那种会导致崩溃的问题。)

关于ios - UIButton setFrame无法识别的选择器alignmentRectInsets,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21127070/

10-13 01:18