0

As far as I know, views displayed by constraints don't got a frame, so what should I do when I want to draw some lines in these views? Methods like moveToPoint do need a CGRect.

Here's my check: NSLog(@"%f,%f,%f,%f",self.contentView.frame.origin.x,self.contentView.frame.origin.y,self.contentView.frame.size.width,self.contentView.frame.size.height); And the result is 0.000000,0.000000,0.000000,0.000000

For more details, here's my code:

-(void)loadView
{
    self.view = [[UIView alloc]init];


    self.titleView = [[UIView alloc]init];
    self.placesHolder = [[UIView alloc]init];
    self.contentView = [[UIView alloc]init];

    self.titleView.translatesAutoresizingMaskIntoConstraints = NO;
    self.placesHolder.translatesAutoresizingMaskIntoConstraints = NO;
    self.contentView.translatesAutoresizingMaskIntoConstraints = NO;

    self.titleView.backgroundColor = [UIColor grayColor];
    self.placesHolder.backgroundColor = [UIColor blueColor];
    self.contentView.backgroundColor = [UIColor redColor];

    [self.view addSubview:self.titleView];
    [self.view addSubview:self.placesHolder];
    [self.view addSubview:self.contentView];

    NSDictionary *timeLineViewMap = @{@"titleView":self.titleView,
                                  @"placesHolder":self.placesHolder,
                                  @"contentView":self.contentView
                                  };


    NSArray *titleHorizon = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[titleView]|" options:0 metrics:nil views:timeLineViewMap];
    NSArray *placesHolderHorizon = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[placesHolder(==58)]-0-[contentView]|" options:0 metrics:nil views:timeLineViewMap];
    NSArray *titleVertical = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[titleView(==58)]-0-[placesHolder]|" options:0 metrics:nil views:timeLineViewMap];
    NSArray *contentViewConstrain = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[titleView]-0-[contentView]|" options:0 metrics:nil views:timeLineViewMap];


    [self.view addConstraints:titleHorizon];
    [self.view addConstraints:placesHolderHorizon];
    [self.view addConstraints:titleVertical];
    [self.view addConstraints:contentViewConstrain];
}
  • To be honest, I really don't know there is a most useful answer function, thanks for telling. –  Dec 12 '14 at 12:45
  • Double-check whether `self.contentView` is `nil` at this time. – DarkDust Dec 12 '14 at 12:57
  • The code you posted is not related to custom view drawing, they are standard `UIView` instances? And since these views don't have an intrinsic content size they might get set to 0 width/height… especially `contentView`. – DarkDust Dec 12 '14 at 13:02
  • that is just a part of my code, in fact I add some custom views to the contentView, but the bounds was still 0. speaking intrinsic content, I didn't really think about that, let me try. –  Dec 12 '14 at 13:09

2 Answers2

2

Of course UIView does have a frame even with AutoLayout. You just don't set the values manually, AutoLayout is doing that for you. Methods like setFrame: are still called. Simply implement drawRect: like you always did.

DarkDust
  • 90,870
  • 19
  • 190
  • 224
  • But when I print the size of the `bounds`, it is `0,0,0,0`, which means the view didn't set `bounds` at all, did I miss something? –  Dec 12 '14 at 12:44
  • Then most likely your constraints forced the view to have a size of `{0, 0}`. Try setting explicit size and width constraints for debugging. – DarkDust Dec 12 '14 at 12:49
  • Did you actually look at the `rect` argument that is passed to `drawRect:`? Or did you look at `self.bounds` (which is what you should look at)? Or something else? Because if some object in the call-chain is `nil` you'll also get the zero-rect. – DarkDust Dec 12 '14 at 12:58
1

Well, problem kind of solved, it seems that a frame of a view don't get a size until it's on the viewDidAppear move. Then I checked some other questions like iOS AutoLayout - get frame size width, which indicate that addConstraints should be put in the viewDidLayoutSubviews method, which is not in the viewController life cycle.

Community
  • 1
  • 1