如何在自动布局视图中使用UIBezierPath? [英] How to use UIBezierPath in a auto layout view?
问题描述
据我所知,约束显示的视图没有框架,那么当我想在这些视图中画线时该怎么办?像 moveToPoint
这样的方法确实需要 CGRect
。
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
.
我的支票: 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
更多详细信息,这是我的代码:
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];
}
推荐答案
当然 UIView
即使使用自动版式也确实具有框架。您只是不需要手动设置值,AutoLayout会为您完成设置。仍会调用 setFrame:
之类的方法。只需像往常一样实现 drawRect:
。
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.
这篇关于如何在自动布局视图中使用UIBezierPath?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!