Button setTitle is not working/ is not visible

2019-04-17 10:54发布

I have a simple problem with my buttons. Title is not visible. My code is :

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"ikonki%d.png",i]] forState:UIControlStateNormal];
    [button setTitle:@"Normal" forState:UIControlStateNormal];
    [button setTitleColor: [UIColor whiteColor] forState: UIControlStateNormal];
    [button setEnabled:YES];

    [button addTarget:self
               action:@selector(buttonHandler:) 
     forControlEvents:UIControlEventTouchUpInside];


    button.tag = i;
    button.frame = CGRectMake(10. + 77. * i, 125., 68 , 68);


    [self.view addSubview:button];

2条回答
Lonely孤独者°
2楼-- · 2019-04-17 11:13

yep, it is normal behaviour when you are using the image property.

use the backgroundImage property instead of the image one, like this:

[button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"ikonki%d.png",i]] forState:UIControlStateNormal];

UPDATE #1:

to reposition the title of the button under the button, use this way:

[button setTitleEdgeInsets:UIEdgeInsetsMake(0.f, 0.f, -60.f, 0.f)]; // align the values for your specific button
[button setClipsToBounds:false];

UPDATE #2:

to change the font of the title, try this one:

[button.titleLabel setFont:[UIFont systemFontOfSize:34.f]]; // set the font size what you desire
查看更多
趁早两清
3楼-- · 2019-04-17 11:18

This can also happen if you forget to use the @synthesize on the button or variables related to it.

查看更多
登录 后发表回答