ios - xib 的 UIControl 在 ios 7.1 上崩溃

标签 ios objective-c crash uicontrol nscoder

我在 Storyboard中创建了一个自定义 UIControl ( MyControl )。在 ios 8 和 ios 9 上一切都很好,我的应用程序没有崩溃。但在 ios 7 和 iphone 4 上,应用程序在尝试加载包含此控件的 View Controller 时崩溃。调试器突出显示这一行:

typeof(self) loadedView = self.viewFromNib;

MyControl.h:

@interface MyControl : ControlFromXib

ControlFromXib.h:

@interface ControlFromXib : UIControl

- (void)commonInit;
@end

ControlFromXib.m:

- (instancetype)viewFromNib
{
    typeof(self) view = [[NSBundle mainBundle] loadNibNamed:self.nibName owner:nil options:nil].firstObject;
    return view;
}

- (NSString *)nibName
{
    return NSStringFromClass([self class]);
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self = self.viewFromNib;
        [self commonInit];
        self.frame = frame;
    }
    return self;
}

- (id)awakeAfterUsingCoder:(NSCoder *)aDecoder
{
    if (self.subviews.count == 0) {
        typeof(self) loadedView = self.viewFromNib;

        loadedView.frame = self.frame;
        loadedView.autoresizingMask = self.autoresizingMask;
        loadedView.translatesAutoresizingMaskIntoConstraints = self.translatesAutoresizingMaskIntoConstraints;

        for (NSLayoutConstraint *constraint in self.constraints) {
            id firstItem = constraint.firstItem;
            if (firstItem == self) {
                firstItem = loadedView;
            }
            id secondItem = constraint.secondItem;
            if (secondItem == self) {
                secondItem = loadedView;
            }
            [loadedView addConstraint:
                    [NSLayoutConstraint constraintWithItem:firstItem
                                                 attribute:constraint.firstAttribute
                                                 relatedBy:constraint.relation
                                                    toItem:secondItem
                                                 attribute:constraint.secondAttribute
                                                multiplier:constraint.multiplier
                                                  constant:constraint.constant]];
        }

        self = loadedView;
    }

    [self commonInit];

    return self;
}

- (void)commonInit
{

}

@end

最佳答案

检查约束属性并与列表 here 进行比较。以下属性是在 iOS 8 中添加的,如果在 xib 或代码中引用,这些属性将在 iOS 7(及更早版本)上崩溃。

  • NSLayoutAttributeLastBaseline
  • NSLayoutAttributeFirstBaseline
  • NSLayoutAttributeLeftMargin
  • NSLayoutAttributeRightMargin
  • NSLayoutAttributeTopMargin
  • NSLayoutAttributeBottomMargin
  • NSLayoutAttributeLeadingMargin
  • NSLayoutAttributeTrailingMargin
  • NSLayoutAttributeCenterXWithinMargins
  • NSLayoutAttributeCenterYWithinMargins

关于ios - xib 的 UIControl 在 ios 7.1 上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33038035/

相关文章:

iOS通知-对象参数可以是任意对象吗?

php - NSURLConnection 连接到服务器,但不发布数据

ios - iPad App - 防止它被安装到 iPad 1

javascript - 无限滚动会导致浏览器崩溃吗?

swift - 为什么这个 Swift UIImage 函数会溢出我的内存?

iphone - iOS - 是否可以在运行时为 uitabbar 选择 View ?

iphone - 如何通过蓝牙将iphone/ipad与其他设备连接?

ios - 复制 Google 的搜索 iOS 卡片动画

objective-c - UITableView dataSource 必须从 tableView :cellForRowAtIndexPath error 返回一个单元格

objective-c - Obj-C 到 Swift 中的实例方法