自定义Facebook按钮图像不居中

时间:2015-06-20 20:04:31

标签: facebook facebook-graph-api uibutton uiimage

我创建了一个自定义的Facebook登录按钮。一切正常但我无法将图像置于按钮的中心(顶部/底部)。它本身的图像,但是当我创建此按钮时,图像较低。

//FB Button (Uses Facebook SDK)
self.fbLoginButton = [[FBLoginView alloc] init];
for (id loginObject in self.fbLoginButton.subviews)
{
    if ([loginObject isKindOfClass:[UIButton class]])
    {
        UIButton * loginButton =  loginObject;
        UIImage *loginImage = [UIImage imageNamed:@"Facebook_White.png"];
        [loginButton setBackgroundImage:loginImage forState:UIControlStateNormal];
        [loginButton setBackgroundImage:nil forState:UIControlStateSelected];
        [loginButton setBackgroundImage:nil forState:UIControlStateHighlighted];
        [loginButton setTitle:nil forState:UIControlStateNormal];
        [loginButton setTitle:nil forState:UIControlStateSelected];
        [loginButton setTitle:nil forState:UIControlStateHighlighted];

    }
    if ([loginObject isKindOfClass:[UILabel class]])
    {
        UILabel * loginLabel =  loginObject;
        loginLabel.text = @"";
        loginLabel.frame = CGRectMake(0, 0, 0, 0);
    }
}
self.fbLoginButton.delegate = self;
self.fbLoginButton.readPermissions = @[@"public_profile", @"email"];

[[self.fbLoginButton layer]setBorderWidth:2.0];
[[self.fbLoginButton layer]setBorderColor:UIColorFromRGB(0xbbffd6).CGColor];

[buttonsArray addObject:self.fbLoginButton];

与Twitter按钮相比,它的外观如下:

enter image description here

这是实际的.png(文字是白色的,所以除非是背景,否则你看不到它:

enter image description here

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

玩完之后,这对我有用。必须硬编码fbLoginView.frame和loginButton.frame。

//FB Button (Uses Facebook SDK)
self.fbLoginView = [[FBLoginView alloc] init];
self.fbLoginView.readPermissions = @[@"public_profile", @"email"];
self.fbLoginView.frame = CGRectMake(0, 0, 35, 35);
[[self.fbLoginView layer]setBorderWidth:2.0];
[[self.fbLoginView layer]setBorderColor:UIColorFromRGB(0xbbffd6).CGColor];

for (id loginObject in self.fbLoginView.subviews)
{
    if ([loginObject isKindOfClass:[UIButton class]])
    {
        UIButton * loginButton =  loginObject;
        loginButton.frame = CGRectMake(self.fbLoginView.frame.origin.x, self.fbLoginView.frame.origin.y, 35, 35);
        UIImage *loginImage = [UIImage imageNamed:@"Facebook_White.png"];
        [loginButton setBackgroundImage:loginImage forState:UIControlStateNormal];
        [loginButton setBackgroundImage:nil forState:UIControlStateSelected];
        [loginButton setBackgroundImage:nil forState:UIControlStateHighlighted];
    }
    if ([loginObject isKindOfClass:[UILabel class]])
    {
        UILabel * loginLabel =  loginObject;
        loginLabel.text = @"";
        loginLabel.frame = CGRectMake(0, 0, 0, 0);
    }
}

self.fbLoginView.delegate = self;
[socialButtonsArray addObject:self.fbLoginView];