如何在自动布局视图中使用UIBezierPath?

时间:2014-12-12 12:26:09

标签: objective-c cocoa-touch flkautolayout

据我所知,约束显示的视图没有框架,所以当我想在这些视图中绘制一些线条时,我该怎么办?像moveToPoint这样的方法确实需要CGRect

这是我的支票: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); 结果是0.000000,0.000000,0.000000,0.000000

有关详细信息,请参阅我的代码:

-(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];
}

2 个答案:

答案 0 :(得分:3)

当然UIView确实有一个框架,即使是AutoLayout。您只是不手动设置值,AutoLayout正在为您执行此操作。仍然会调用setFrame:之类的方法。只需像往常一样实施drawRect:

答案 1 :(得分:1)

嗯,问题解决了,似乎视图的框架在viewDidAppear移动之前没有大小。然后我检查了一些其他问题,例如iOS AutoLayout - get frame size width,这表明addConstraints应该放在viewDidLayoutSubviews方法中,而不是在viewController生命周期中。