我的项目中有以下 Xib 文件:
我试图向提示 View 添加一些新的约束( super View 的底部、顶部、左侧和右侧空间),但是从界面生成器中这是不可能的,如图所示。应该怎么做?
对应函数:
+ (instancetype)presentInViewController:(UIViewController *)viewController withDefaultsKey:(NSString *)defaultsKey {
ELHintViewOwner *owner = [ELHintViewOwner new];
NSArray *bundle = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:owner options:nil];
//hintViewOwner.decoupledView.delegateViewController = viewController
ELHintView *hintView = [bundle firstObject];
hintView.frame = viewController.view.bounds;
hintView.titleLabel.text = @"";
hintView.defaultsKey = defaultsKey;
hintView.tapAnywhereLabel.text = NSLocalizedString(@"Tap anywhere to continue", nil);
hintView.showLabel.text = NSLocalizedString(@"Don't show this message again", nil);
hintView.imageView.hidden = YES;
hintView.showSwitch.on = ![[[NSUserDefaults standardUserDefaults] valueForKey:defaultsKey] boolValue];
if ([hintView shouldShow])
[viewController.view addSubview:owner.decoupledView];
return hintView;
}
最佳答案
如果要将父 View 添加到其他 View ,则必须在代码中添加自动布局约束。
+ (instancetype)presentInViewController:(UIViewController *)viewController withDefaultsKey:(NSString *)defaultsKey {
ELHintViewOwner *owner = [ELHintViewOwner new];
NSArray *bundle = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:owner options:nil];
//hintViewOwner.decoupledView.delegateViewController = viewController
ELHintView *hintView = [bundle firstObject];
hintView.frame = viewController.view.bounds;
hintView.titleLabel.text = @"";
hintView.defaultsKey = defaultsKey;
hintView.tapAnywhereLabel.text = NSLocalizedString(@"Tap anywhere to continue", nil);
hintView.showLabel.text = NSLocalizedString(@"Don't show this message again", nil);
hintView.imageView.hidden = YES;
hintView.showSwitch.on = ![[[NSUserDefaults standardUserDefaults] valueForKey:defaultsKey] boolValue];
if ([hintView shouldShow]){
[viewController.view addSubview:owner.decoupledView];
hintView.translatesAutoresizingMaskIntoConstraints = NO;
NSArray *constraintsX = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[hintView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(hintView)];
NSArray *constraintsY = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[hintView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(hintView)];
[viewController.view addConstraints:constraintsX];
[viewController.view addConstraints:constraintsY];
}
return hintView;
}
关于ios - 从 xib 文件加载的自动调整 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23648757/