0

UPDATE: Corrected the text and image of the button properties.

I am trying to show a button if specific information is available and have that button perform an action when clicked; Otherwise, just show a label. I have created the following:

if(rec.biography != nil)
{
    btnProviderBio = [UIButton buttonWithType:UIButtonTypeCustom];

    [btnProviderBio setImage:[UIImage imageNamed:@"info.png"] forState:UIControlStateNormal];
    [btnProviderBio setTitle:[NSString stringWithFormat:@"%@ - %@", rec.providerName, rec.facilityName] forState:UIControlStateNormal];
    [btnProviderBio setFrame: CGRectMake(13.0, yValue, [UIScreen mainScreen].bounds.size.width-26, 40.0)];
    [btnProviderBio addTarget:self action:@selector(providerAlertDiaglog:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnProviderBio];
}
else{
    lblFacilityProvider = [[UILabel alloc] init];
    [lblFacilityProvider setFont:[UIFont systemFontOfSize: 14]];
    [lblFacilityProvider setText:[NSString stringWithFormat:@"%@ - %@", rec.providerName, rec.facilityName]];
    [lblFacilityProvider setFrame: CGRectMake(13.0, yValue, [UIScreen mainScreen].bounds.size.width-26, 40.0)];
    [self.view addSubview:lblFacilityProvider];
}

I get the below result now. No label text and the button at random places. I would like for the button to be on the left with the text on the right.

Deplay results

  • Clarify your question. Is there a problem with the button code or is your issue that `rec.biography` is always `nil`? – rmaddy Sep 18 '17 at 20:19
  • Problem with button code. Button nor title label ever appear and the row is blank. I believe there is an issue with the self.view taking the row. Not sure what I am missing. – Andrew Smith Sep 18 '17 at 20:24
  • That's not the proper way to set the title of a button. Use the `setTitle:forState:` method. – rmaddy Sep 18 '17 at 20:26
  • I will correct that and the way I call the image as I don't believe that is the proper way either. Will update the post with edited code to reflect that and let you know if that was the issue. – Andrew Smith Sep 18 '17 at 20:37
  • Updated with your suggestion and a screenshot of the current state. – Andrew Smith Sep 18 '17 at 20:48
  • If your issue is trying to create a button with both a label and an image then this is a duplicate of https://stackoverflow.com/questions/11717219/uibutton-image-text-ios – rmaddy Sep 18 '17 at 20:57
  • PLEASE ADD TINT COLOR, I think your problem will solved – Vivek Sep 18 '17 at 21:02
  • The problem here is because btn image and texts are center aligned. That is why you have your i icon appear to be random position – GeneCode Sep 19 '17 at 00:01

1 Answers1

0

I think you should use setTitleTintColor, because Custom button's default titleTintColor value is white, so you need to set titleTintColor. Please check below code.

if(rec.biography != nil)
{
    btnProviderBio = [UIButton buttonWithType:UIButtonTypeCustom];

    [btnProviderBio setImage:[UIImage imageNamed:@"info.png"] forState:UIControlStateNormal];
    [btnProviderBio setTitle:[NSString stringWithFormat:@"%@ - %@", rec.providerName, rec.facilityName] forState:UIControlStateNormal];
    [btnProviderBio setFrame: CGRectMake(13.0, yValue, [UIScreen mainScreen].bounds.size.width-26, 40.0)];
    [btnProviderBio addTarget:self action:@selector(providerAlertDiaglog:) forControlEvents:UIControlEventTouchUpInside];
     // I used rgb color but you can use your color, this rgb color just for your reference.
     [btnProviderBio setTitleColor:[UIColor colorWithRed:36/255.0 green:71/255.0 blue:113/255.0 alpha:1.0] forState:UIControlStateNormal];

    [self.view addSubview:btnProviderBio];
}
else{
    lblFacilityProvider = [[UILabel alloc] init];
    [lblFacilityProvider setFont:[UIFont systemFontOfSize: 14]];
    [lblFacilityProvider setText:[NSString stringWithFormat:@"%@ - %@", rec.providerName, rec.facilityName]];
    [lblFacilityProvider setFrame: CGRectMake(13.0, yValue, [UIScreen mainScreen].bounds.size.width-26, 40.0)];
    [self.view addSubview:lblFacilityProvider];
}
Vivek
  • 4,916
  • 35
  • 40
  • Please don't post code with no explanation of any kind. A good answer explains what was wrong and how the answer solves the issue. – rmaddy Sep 18 '17 at 21:46