小编Tob*_*ler的帖子

在ViewController的loadView中创建的自动调整视图的AutoLayout约束

UIViewController通过覆盖loadView方法创建它的视图:

- (void)loadView {
    UIView *view = [[UIView alloc] init];
    view.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    self.view = view;
}
Run Code Online (Sandbox Code Playgroud)

现在我想切换到AutoLayout,因此添加一个

view.translatesAutoresizingMaskIntoConstraints = NO;
Run Code Online (Sandbox Code Playgroud)

到loadView方法.现在我必须指定之前自动生成的相同约束.我的方法是覆盖updateViewConstraints

- (void)updateViewConstraints {
    if (0 == [[self.view constraints] count]) {
        NSDictionary* views = @{@"view" : self.view};

        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view]|" options:0 metrics:0 views:views]];
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view]|" options:0 metrics:0 views:views]];
     }

    [super updateViewConstraints];
}
Run Code Online (Sandbox Code Playgroud)

但我得到一个例外,因为我认为这种约束应该与超级视图一致:

*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to install constraint on view.  Does the constraint reference something from outside the …
Run Code Online (Sandbox Code Playgroud)

objective-c ios autolayout ios6

8
推荐指数
3
解决办法
1万
查看次数

返回与 C++ 中的类相同的类型

我想要一个 C++ 类方法来返回对同类对象的引用。这也适用于派生类。在下面的示例中,我简单地将“A*”作为函数的返回类型,但这在使用类 B 时显然是一个问题。

\n\n
class A {\npublic:\n    A* self() { return this; };\n};\n\nclass B: public A {\n\n};\n\nint main(int argc, char **argv) {\n    A* a1 = new A;\n    B* b1 = new B;\n\n    A* a2 = a1->self();\n    B* b2 = b1->self();\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

编译器抱怨

\n\n
\n

class.cpp:在函数 \xe2\x80\x98int main(int, char**)\xe2\x80\x99 中:class.cpp:16: 错误:
\n 来自 \xe2\x80\x98A*\xe2\ 的无效转换x80\x99 到 \xe2\x80\x98B*\xe2\x80\x99

\n
\n\n

在 Objective-C 中我会使用instanceof\n 特殊类型作为返回类型。对于此类问题,是否有类似于 C++ 中使用的内容或任何已知模式?

\n

c++

2
推荐指数
1
解决办法
1417
查看次数

标签 统计

autolayout ×1

c++ ×1

ios ×1

ios6 ×1

objective-c ×1