How to customize FBLoginVIew? How to customize FBLoginVIew? ios ios

How to customize FBLoginVIew?


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];


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];    }  }}


When I'm writing this, Facebook has already released version 3.0 of their SDK for iOS.

ozba's answer is sort of working but, from my opinion, it shouldn't be used.

  • Firstly, and most important, it's a general rule that it's bad practice to iterate through the subviews/superviews of an SDK component because the hierarchy can change between the SDK versions, causing errors that will be hard to track and, then, repair.
  • Secondly, when the button is clicked, for a second or two, it comes back to the original text.

My solution is really simple. In Facebook's downloaded folder, FacebookSDK, you have a folder named Samples. There, you must look in the SessionLoginSample. You can see that they have an ordinary Round Rect Button, which, through the ViewController that owns it, that has the FBLoginViewDelegate, it can change text depending on the FBSession state. Trust me, it's really simple.