在PFLogInViewController中添加按钮



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

谢谢!

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

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

@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");
}

最新更新