gpt4 book ai didi

ios - xib 的 UIControl 在 ios 7.1 上崩溃

转载 作者:行者123 更新时间:2023-12-01 23:30:53 32 4
gpt4 key购买 nike

我在 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/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com