-2

次のコードがあります。

- (void)addLogoutButton {
    UIButton *logoutButton = [[UIButton alloc] init];
    logoutButton.translatesAutoresizingMaskIntoConstraints = NO;
    [logoutButton setTitle:@"Logout" forState:UIControlStateNormal];
    [self.view addSubview:logoutButton];

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:logoutButton
                                                 attribute:NSLayoutAttributeTop
                                                 relatedBy:NSLayoutRelationEqual
                                                 toItem:self.view
                                                 attribute:NSLayoutAttributeTop
                                                 multiplier:1.0
                                                 constant:-100.0]];

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:logoutButton
                                                 attribute:NSLayoutAttributeLeft
                                                 relatedBy:NSLayoutRelationEqual
                                                 toItem:self.view
                                                 attribute:NSLayoutAttributeLeft
                                                 multiplier:1.0
                                                 constant:40.0]];


    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:logoutButton
                                                 attribute:NSLayoutAttributeRight
                                                 relatedBy:NSLayoutRelationEqual
                                                 toItem:self.view
                                                 attribute:NSLayoutAttributeRight
                                                 multiplier:1.0
                                                 constant:-40]];
}

しかし、アプリを実行すると、ボタンがどこにも見つかりません。どうしてこれなの?

4

1 に答える 1

1

最初の制約定数を次のように変更し+100.0、ボタンを初期化します。

[UIButton buttonWithType:UIButtonTypeRoundedRect];
于 2013-06-29T02:33:21.430 に答える