How to customize FBLoginVIew?

后端 未结 17 845
暗喜
暗喜 2020-12-02 05:56

In order to connect to facebook in my ios app i\'m using FBLoginVIew from Facebook SDK for iOS.

It shows a nice FB Login Button, but I want to use my own image and t

相关标签:
17条回答
  • 2020-12-02 06:32

    The answer is to go over the FBLoginView subviews, find the button and the label and customize them.

    Here is the code:

    FBLoginView *loginview = 
    [[FBLoginView alloc] initWithPermissions:[NSArray arrayWithObject:@"publish_actions"]];
    
    
    loginview.frame = CGRectMake(4, 95, 271, 37);
    for (id obj in loginview.subviews)
            {
                if ([obj isKindOfClass:[UIButton class]])
                {
                    UIButton * loginButton =  obj;
                    UIImage *loginImage = [UIImage imageNamed:@"YourImg.png"];
                    [loginButton setBackgroundImage:loginImage forState:UIControlStateNormal];
                    [loginButton setBackgroundImage:nil forState:UIControlStateSelected];
                    [loginButton setBackgroundImage:nil forState:UIControlStateHighlighted];
                    [loginButton sizeToFit];
                }
                if ([obj isKindOfClass:[UILabel class]])
                {
                    UILabel * loginLabel =  obj;
                    loginLabel.text = @"Log in to facebook";
                    loginLabel.textAlignment = UITextAlignmentCenter;
                    loginLabel.frame = CGRectMake(0, 0, 271, 37);
                }
            }
    
    loginview.delegate = self;
    
    [self.view addSubview:loginview];
    
    0 讨论(0)
  • 2020-12-02 06:32

    The answer to your all problems, including the label after log-in pressed is very simple: It's like OZBA answer just remove completely the UILabel (add: [loginLabel removeFromSuperview];)

    This is the full working code:

    - (void)setFacebookConnectButton{
        self.loginView.readPermissions = @[@"public_profile", @"email", @"user_friends"];
        [self.loginView setDelegate:self];
        for (id obj in self.loginView.subviews)
        {
            if ([obj isKindOfClass:[UIButton class]])
            {
                UIButton * loginButton =  obj;
                UIImage *loginImage = [UIImage imageNamed:@"fb_connect"];
                [loginButton setBackgroundImage:loginImage forState:UIControlStateNormal];
                [loginButton setBackgroundImage:nil forState:UIControlStateSelected];
                [loginButton setBackgroundImage:nil forState:UIControlStateHighlighted];
                [loginButton setTitle:nil forState:UIControlStateSelected];
                [loginButton setTitle:nil forState:UIControlStateHighlighted];
    
                [loginButton sizeToFit];
            }
            if ([obj isKindOfClass:[UILabel class]])
            {
                UILabel * loginLabel =  obj;
                loginLabel.text = @"";
                loginLabel.textAlignment = NSTextAlignmentCenter;
                loginLabel.frame = CGRectMake(0, 0, 271, 37);
                [loginLabel removeFromSuperview];
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-02 06:34

    Or read the documentation of Facebook to build Your Own Button:

    https://developers.facebook.com/docs/ios/login-tutorial/#login-apicalls

    like this:

    - (void) buttonSelector
    {
    [FBSession openActiveSessionWithReadPermissions:@[@"basic_info"]
                                           allowLoginUI:YES
                                      completionHandler:
         ^(FBSession *session, FBSessionState state, NSError *error) {
    
             if (FBSession.activeSession.state == FBSessionStateOpen) {
    
                 //To get the use
                 [[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection,NSDictionary<FBGraphUser> *user, NSError *error) {
                     NSString *token = [[[FBSession activeSession] accessTokenData] accessToken];
    
    
                 }];
    
             }
         }];
    
    }
    
    0 讨论(0)
  • 2020-12-02 06:35

    I just found an answer for the button text updating when pressing the login button. I suffered the same problem.

    How about try setting the frame of the button label to CGRectMake(0,0,0,0).

    I tried it and I think it worked.

    Add this code to ozba's answer:

    [loginLabel setFrame:CGRectMake(0, 0, 0, 0)];
    

    Hope this helps.

    0 讨论(0)
  • 2020-12-02 06:36

    I thought about the problem, and I personally didn't really liked the accepted answer... For various reason. Everybody is free whatever solution he likes, but I decided to share my own solution.

    for (UIButton *view in loginView.subviews) {
        if ([view respondsToSelector:@selector(addTarget:action:forControlEvents:)]) {
            [view setBackgroundImage:nil forState:UIControlStateNormal];
            [view setBackgroundImage:nil forState:UIControlStateHighlighted];
        }
    }
    

    I used respondsToSelector instead of isKindOfClass because the class might be custom in the future.

    0 讨论(0)
  • 2020-12-02 06:41

    I wanted to be FBLoginview shows as a UIBarbutton, So i have made a simple approach:

    First i've added FBLoginView as a property:

    @property (nonatomic, strong) FBLoginView* loginView;
    

    And then i've added the login view outside the view:

        self.loginView = [[FBLoginView alloc] init];
        self.loginView.frame = CGRectMake(-500, -500, 0, 0);
        [self.view addSubview:self.loginView];
        self.loginView.delegate = self;
    

    And then, i've added a standard system UIbarbuttonItem

        UIBarButtonItem* fbButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(fireFbLoginView)];
        [self.navigationItem setRightBarButtonItem:fbButton];
    

    And then i set the bar button to fire click action of the FBLoginView ;)

    -(void)fireFbLoginView{
    for(id object in self.loginView.subviews){
        if([[object class] isSubclassOfClass:[UIButton class]]){
            UIButton* button = (UIButton*)object;
            [button sendActionsForControlEvents:UIControlEventTouchUpInside];
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题