【问题标题】:Custom UIView storyboard class not created in simulator, shows up in interface builder自定义 UIView 故事板类未在模拟器中创建,显示在界面构建器中
【发布时间】:2016-12-26 10:48:49
【问题描述】:

我正在尝试制作一个基本上是圆形进度条的自定义故事板组件。我已经创建了继承 UIView 的自定义类(称为 CircularProgressView)。然后我在界面生成器中创建一个 UIView 并使其成为我的 CircualProgressView 类的实例。一切都在 Interface Builder 中按预期呈现,但是当我在模拟器或我的设备(iPhone 6)中加载应用程序时,它根本不存在。我尝试在函数initWithFrameinitWithCoderdrawRect 中设置断点,但没有达到断点。为什么??

非常感谢任何帮助!

【问题讨论】:

  • 检查您的自定义类是否以编程方式重新创建 UIView 并且不会丢弃您的故事板 xib
  • 你能验证 ViewController 正在加载吗?制作一个 IBOutlet 并验证它是否存在?

标签: ios xcode cocoa-touch uiview interface-builder


【解决方案1】:

我曾经遇到过这个问题,所以我在 UIView 类中做了什么,我编写了我的自定义初始化程序,如下所示:

-(id) initAndConfigure{
    if (![self.subviews count])
    {
        NSBundle *mainBundle = [NSBundle mainBundle];
        NSArray *loadedViews = [mainBundle loadNibNamed:@"YourNibName" owner:nil options:nil];
        PlayerControlView *loadedView = [loadedViews firstObject];

        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]];
        }
        return loadedView;
    }
    [self configureView];
    return self;
}

[self configureView]函数用于配置其他视图项。

在.xib 中制作 UIView 时会出现此问题,如果有人尝试 在其他一些 .xib 中使用此视图。

希望对你有所帮助。

【讨论】:

  • 您好!感谢您的回答!我目前不在工作区,但稍后会尝试您的解决方案。
猜你喜欢
  • 2017-12-06
  • 1970-01-01
  • 1970-01-01
  • 2014-03-25
  • 2013-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-22
相关资源
最近更新 更多