ios - 从 xib 文件加载的自动调整 View

标签 ios iphone objective-c interface-builder xib

我的项目中有以下 Xib 文件: XIB file

我试图向提示 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/

相关文章:

ios - 如何使用tableview删除核心数据中的数据

iphone - 我想设置一个任意键 :value property on a UIView

iphone - 完整的状态列表,我们可以通过 iDevice 以编程方式查询

iphone - 如何检测 iPad 上启动屏幕动画的应用程序启动方向!

ios - NSUserDefaults 自定义对象 - 格式 : 200 (property lists cannot contain objects of type 'CFType' ) 的属性列表无效

objective-c - "Expected a Type"编译器应该知道的协议(protocol)错误

ios - 如何在 Interface Builder 中使用自动布局创建键盘附件 View ?

ios - 在 iOS 4 中使用 block 对一系列图像进行动画处理

ios - 使用子类化 UIActivityItemProvider 时没有可用的共享操作

ios - 无法在 Swift 中的其他 View Controller 中创建 View Controller 实例