-1

I am trying to duplicate this programmatically:

enter image description here

I have seen there is a lot of confusion with how Apple handles this. I tried the following, but it doesn't work for me:

[buttonTop setAutoresizingMask:UIViewAutoresizingFlexibleHeight];

Any ideas where to go from here?

Update:

The code for my button is as follows:

buttonTop = [UIButton buttonWithType:UIButtonTypeCustom];
buttonTop.frame = CGRectMake(0, 0, 100, 100);
buttonTop.center = self.view.center;
buttonTop.layer.cornerRadius = buttonTop.frame.size.width / 2;
buttonTop.clipsToBounds = YES;
buttonTop.layer.masksToBounds = YES;
[buttonTop setAutoresizingMask:UIViewAutoresizingFlexibleHeight];

if (GetUserProPic){
    [buttonTop sd_setImageWithURL:[NSURL URLWithString:GetUserProPic] forState:UIControlStateNormal];
    [buttonTop sd_setImageWithURL:[NSURL URLWithString:GetUserProPic] forState:UIControlStateSelected];
} else {
    [buttonTop setImage:[UIImage imageNamed:@"avatar_icon"] forState:UIControlStateNormal];
    [buttonTop setImage:[UIImage imageNamed:@"avatar_icon"] forState:UIControlStateSelected];
}

[self.view addSubview:buttonTop];

The output looks like this:

enter image description here

However, I would like it to look like this:

enter image description here

jape
  • 2,861
  • 2
  • 26
  • 58
  • You shouldn't be using auto resizing masks any more except in legacy apps, and even there, you should really update them to Auto-Layout. – Duncan C Feb 17 '17 at 15:35
  • @DuncanC I disagree. There are various tools available. Use what works in a given situation. There is nothing wrong with autoresizing masks. – rmaddy Feb 17 '17 at 15:36
  • @jape You need to clarify what you mean by "doesn't work". And provide more details about how the button is created and setup. Put all of this info into your question, not in comments. – rmaddy Feb 17 '17 at 15:37
  • Try setting translatesAutoresizingMaskIntoConstraints to No – Sahana Kini Feb 17 '17 at 15:47
  • @rmaddy I just updated my question. Thank you! – jape Feb 17 '17 at 16:09
  • @SahanaKini Unfortunately, that didn't work. – jape Feb 17 '17 at 16:10
  • @jape Your "music" images seem to indicate an issue with the width but your code and setup is only dealing with the height. – rmaddy Feb 17 '17 at 16:11
  • "I am trying to duplicate this programmatically" Why? – matt Feb 17 '17 at 16:16
  • @rmaddy I had the same issue with other images and that was a solution that worked for those, so I assumed it would work for this as well. If you have another suggestion, I'm open to it! – jape Feb 17 '17 at 19:53
  • @matt The button is generated programmatically, so I cannot edit it with the Storyboard editor. – jape Feb 17 '17 at 19:53
  • Fine. You can do anything in code that you can do with the storyboard editor. However, this does not explain why you want to use the `autoresizingMask` to configure this object. – matt Feb 17 '17 at 20:11
  • @matt I don't know how else to prevent the distortion of the image. If you have any ideas, I'd love to hear them! – jape Feb 17 '17 at 20:15
  • I don't have any "ideas" because you have not at all explained what's going on or what you're trying to do. You asked some irrelevant question about autoresizing, instead of asking about what you are _really_ trying to achieve. In your screen shot, what is the "MUSIC" word and what is the black circle? Are they two different objects? What? Try to provide some useful explanation of what you're trying to do (as you were asked to do several hours ago). – matt Feb 17 '17 at 20:15
  • @matt I'm not sure what kind of other context you're looking for. The code clearly shows the "black circle" is a button with an image ("music word") on it. The image is not being scaled properly to fit in the button. Instead of implying I didn't ask a good question, maybe you could ask for clarification considering everyone else seemed to understand the issue. – jape Feb 17 '17 at 20:26

1 Answers1

0

The button's autoResizeMask affects the button's size when the view containing the button changes size. It does not affect the content inside the button. (And I think the default settings will shrink the image as needed, but not expand it.)

Try code from this SO answer: How do I scale a UIButton's imageView?

buttonTop.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
Community
  • 1
  • 1
Walt Sellers
  • 3,806
  • 30
  • 35