【问题标题】:How to use UIBezierPath in a auto layout view?如何在自动布局视图中使用 UIBezierPath?
【发布时间】:2014-12-12 12:26:08
【问题描述】:

据我所知,约束显示的视图是没有框架的,那么当我想在这些视图中画一些线时该怎么办呢?像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];
}

【问题讨论】:

  • 说实话,我真的不知道有什么最实用的答题功能,谢谢告知。
  • 再次检查self.contentView此时是否为nil
  • 您发布的代码与自定义视图绘图无关,它们是标准的UIView 实例?而且由于这些视图没有固有的内容大小,它们可能会设置为 0 宽度/高度……尤其是contentView
  • 那只是我代码的一部分,实际上我在contentView中添加了一些自定义视图,但是bounds仍然是0。说到内在内容,我并没有真正考虑过,让我试试看。

标签: objective-c cocoa-touch flkautolayout


【解决方案1】:

当然UIView 即使使用自动布局也有框架。您只是不手动设置值,AutoLayout 正在为您执行此操作。像setFrame: 这样的方法仍然被调用。像往常一样简单地实现drawRect:

【讨论】:

  • 但是当我打印bounds的大小时,它是0,0,0,0,这意味着视图根本没有设置bounds,我错过了什么吗?
  • 那么很可能你的约束迫使视图的大小为{0, 0}。尝试为调试设置明确的大小和宽度约束。
  • 您是否真的查看了传递给drawRect:rect 参数?或者你看过self.bounds(这是你应该看的)?或者是其他东西?因为如果调用链中的某个对象是nil,你也会得到零矩形。
【解决方案2】:

好吧,问题已经解决了,似乎视图的框架在 viewDidAppear 移动之前没有大小。然后查了iOS AutoLayout - get frame size width之类的其他一些问题,表示addConstraints应该放在viewDidLayoutSubviews方法中,不在viewController生命周期中。

【讨论】:

    猜你喜欢
    • 2015-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多