JFortYork
JFortYork

Reputation: 147

UIButton appearing incorrectly

I'm having a lot of difficulty trying to figure out how to make a UIButton look right. Essentially, it's supposed to look this this:

enter image description here

The UIButton is setup in a storyboard and has the appropriate constraints. Then, in the code I'm trying to customize it specifically for my use case. Here's the code from my method:

- (void)setupSkipBreakButton
{
    NSString* buttonText = BMStringInApplicationLanguageForKey(skipIntroTextKey);;
    [self.skipBreak setTitle: buttonText forState: UIControlStateNormal];
    UIImage *skipBreak = [UIImage bm_imageWithBundlePath:BMVideoPlayerControlRackViewSkipBreakImageName];
    
    
    [self.skipBreak setTitleEdgeInsets:UIEdgeInsetsMake(3.0, 1.0, 3.0, 0.0)];
    [self.skipBreak setImageEdgeInsets:UIEdgeInsetsMake(3.0, 0.0, 3.0, 2.0)];
    [self.skipBreak setImage:skipBreak forState:UIControlStateNormal];
    self.skipBreak.backgroundColor = [[UIColor bm_colorWithHexString:BMVideoPlayerDefaultColor]
                                                   colorWithAlphaComponent:BMVideoPlayerDefaultAlpha];
    [self.skipBreak addTarget:self action:@selector(skipBreakButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
}

Instead, the button actually has the 2 right arrows on the left side of "Skip Intro" when it should be on the right side as per the above image.

I don't see anything in my code that involves positioning the image as opposed to the text (other than the UIEdgeInsets?). So, I don't understand how to fix this issue.

Please help?

Upvotes: 0

Views: 45

Answers (1)

Sanoj Kashyap
Sanoj Kashyap

Reputation: 5060

Here is code for doing what you want. Basically you need to check the value which you are passing in UIEdgeInsetsMake.

 UIImage *skipBreak = [UIImage imageNamed:@"testpic.png"];
    [self.button setTitle:@"Tesst" forState:UIControlStateNormal];
    [self.button setTitleEdgeInsets:UIEdgeInsetsMake(0.0, 0.0, 0.0, 30.0)];
    [self.button setImage:skipBreak forState:UIControlStateNormal];
    [self.button setImageEdgeInsets:UIEdgeInsetsMake(0.0, 0.0, 0.0, -57)];
    [self.button setBackgroundColor:[UIColor yellowColor]];

which result is below:

enter image description here

I would suggest if you still not able to set the image the best and easy way to use the storyboard.

Step:

  1. Add a button on the storyboard
  2. Add A image in the button

Now use these options and play with them. Best way to learn. enter image description here

Upvotes: 1

Related Questions