0

嗨,我们可以在创建自定义视图时添加一个额外的按钮吗?我为 PFLogInViewController 创建了一个子类。该按钮已添加,但我无法单击该按钮。是否允许在 PFLogInViewController 子类中添加其他按钮?

谢谢!

4

1 回答 1

1

要添加按钮(或任何其他视图),您需要按照本教程https://parse.com/tutorials/login-and-signup-views中的描述子类化 PFLogInViewController

例如,如果您希望将 1Password 按钮添加到 Parse 登录视图的密码字段,您可以这样做:

@interface MyParseLogInViewController : PFLogInViewController

@end

@implementation MyParseLogInViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Add 1Password button
    UIButton *btn1Password = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn1Password addTarget:self action:@selector(onePasswordButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    [btn1Password setImage:[UIImage imageNamed:@"onepassword-button"] forState:UIControlStateNormal];
    [self.logInView.passwordField addSubview:btn1Password];

    // Add constraints to align it to the right.
    btn1Password.translatesAutoresizingMaskIntoConstraints = NO;
    [self.logInView addConstraint:[NSLayoutConstraint constraintWithItem: btn1Password
                                                               attribute: NSLayoutAttributeCenterY
                                                               relatedBy: NSLayoutRelationEqual
                                                                  toItem: self.logInView.passwordField
                                                               attribute: NSLayoutAttributeCenterY
                                                              multiplier: 1.0
                                                                constant: 0.0]];
    [self.logInView addConstraint:[NSLayoutConstraint constraintWithItem: btn1Password
                                                               attribute: NSLayoutAttributeTrailing
                                                               relatedBy: NSLayoutRelationEqual
                                                                  toItem: self.logInView.passwordField
                                                               attribute: NSLayoutAttributeTrailing
                                                              multiplier: 1.0
                                                                constant: -5.0]];
    [self.logInView addConstraint:[NSLayoutConstraint constraintWithItem: btn1Password
                                                               attribute: NSLayoutAttributeWidth
                                                               relatedBy: NSLayoutRelationEqual
                                                                  toItem: nil
                                                               attribute: NSLayoutAttributeNotAnAttribute
                                                              multiplier: 1.0
                                                                constant: 48.0]];
    [self.logInView addConstraint:[NSLayoutConstraint constraintWithItem: btn1Password
                                                               attribute: NSLayoutAttributeHeight
                                                               relatedBy: NSLayoutRelationEqual
                                                                  toItem: nil
                                                               attribute: NSLayoutAttributeNotAnAttribute
                                                              multiplier: 1.0
                                                                constant: 32.0]];
}

- (void)onePasswordButtonTapped:(id)sender
{
    NSLog(@"Button tapped");
}
于 2014-11-16T00:42:50.803 回答