0

I've found and configured a UIView that works perfectly for what I'm trying to do, however I'm having a tough time configuring the CGRect for it.

What I've got:

1) MyViewController.h/m

2) CustomUIView.h/m

In interface builder, I added a UIView object and configured the AutoLayout constraints. In identity inspector, I set Custom Class to CustomUIView. I added an IBOutlet to MyViewController.m.

@property (strong, nonatomic) IBOutlet CustomUIView *customUIView;

In MyViewController's viewDidLoad, I use these commands to add customUIView:

[self.customView drawCustomViewInView:self.customUIView];

In CustomUIView.m, drawCustomViewInView has the following line, which I'm having a b***h of a time with:

customViewWidth = [UIScreen mainScreen].bounds.size.width - (2 * STEPPER_MARK_HEIGHT_WIDTH);

Originally, the values for drawing CustomUIView were hard-coded, so I just made the UIView object the width of the screen and called it a day. That came back to bite me in the @$$ when I started testing different screen sizes.

How would I get CustomUIView to know about the rect I want it to draw in on MyViewController? I know this isn't that hard, but I can't ****ing figure it out. I've discovered [UIScreen mainScreen] won't get the job done for every screen size...I need to tell it the rect.

Adrian
  • 16,233
  • 18
  • 112
  • 180
  • 2
    Did you set autolayout for `customUIView`? if yes, You need set `translatesAutoresizingMaskIntoConstraints = YES` then `setFrame:`. However, when you do this, you still need to make sure that these automatic constraints can be satisfied with the rest of your constraints. – Luan Nguyen Oct 01 '15 at 03:33
  • 1
    More here:http://stackoverflow.com/questions/13186908/can-i-use-setframe-and-autolayout-on-the-same-view – Luan Nguyen Oct 01 '15 at 03:34
  • Thank you! This looks very promising! I'll tinker with it in the morning and let you know how it goes. – Adrian Oct 01 '15 at 03:40
  • THAT WAS IT! This is me right now--> https://www.youtube.com/watch?v=IS7Og1zvdy8 Thank you for your help on this. Throw that up as an answer and I'll get you 25 points :) – Adrian Oct 01 '15 at 03:51
  • If you are adding the CustomView object through IB why are you adding another one programatically? – Paulw11 Oct 01 '15 at 04:32
  • @Paulw11 Luan got me squared away. CustomView has a method that draws in a rect on MyViewController. – Adrian Oct 01 '15 at 05:01
  • 1
    Yes, I understand that, but I still wonder why you are adding the object programatically via `addSubView` if you have already added it via Interface Builder. – Paulw11 Oct 01 '15 at 05:10
  • Thank you for the feedback. It might be a stray line from when I was messing around with it. I'll nuke it and see what happens. Thank you for raising this point. – Adrian Oct 01 '15 at 05:15

1 Answers1

0

Adding two lines to MyViewController's viewDidLoad got me squared away:

self.customUIView.translatesAutoResizingMaskIntoConstraints = YES;
[self.customUIView setFrame:self.customUIView.frame];

Those two lines enabled me to incorporate a custom UIView with its own drawing methods into a UIViewController via AutoLayout. Special thanks to Luan Nguyen for pointing me in the right direction.

A more in-depth discussion of this topic can be found in "How I Learned to Stop Worrying and Love AutoLayout".

Adrian
  • 16,233
  • 18
  • 112
  • 180