在视图上显示自定义行

时间:2015-07-01 12:25:07

标签: ios objective-c

嗨我想在给定图像的过度视图中显示一条线。

enter image description here

但是当我这样做时,我会在完整视图中排队。

我的代码 -

UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(2,30, _MovetoSearch.frame.size.width, 1)];
lineView.backgroundColor = [UIColor whiteColor];
[_MovetoSearch addSubview:lineView];

如何制作如上图所示的线条?

3 个答案:

答案 0 :(得分:0)

当您设置子视图框时,总是使用父视图框设置它,所以如果以后您可以更改父框架,则无需更改子框架,它会根据父视图自动更改,如下所示。

UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(_MovetoSearch.frame.origin.x+2,_MovetoSearch.frame.size.height-5, _MovetoSearch.frame.size.width-30, 1)];
    lineView.backgroundColor = [UIColor whiteColor];
    [_MovetoSearch addSubview:lineView];

答案 1 :(得分:0)

我认为你的searchBtn有类似的代码

UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[searchBtn setFrame:CGRectMake(_MovetoSearch.frame.size.width-32, 0, 32, 32)];    
[searchBtn setImage:[UIImage imageNamed:@"search.png"] forState:UIControlStateNormal];
[searchBtn setImage:[UIImage imageNamed:@"search.png"] forState:UIControlStateHighlighted];
[searchBtn addTarget:self action:@selector(searchBtnPressed) forControlEvents:UIControlEventTouchUpInside];

<击> 对于您的UITextField

CGFloat fieldHeight = searchBtn.frame.size.height-2;
CGFloat fieldWidth = _MovetoSearch.frame.size.width - (searchBtn.frame.size.width + 4);

UITextField *textfield = [[UITextField alloc] initWithFrame:CGRectMake(2, 0, fieldWidth, fieldHeight)];
[textfield setBackgroundColor:[UIColor clearColor]];
[textfield setBorderStyle:UITextBorderStyleNone];
[textfield setNeedsDisplay];
[_MovetoSearch addSubview:textfield];

用于线视图

CGFloat fieldHeight = searchBtn.frame.size.height-2;
CGFloat fieldWidth = _MovetoSearch.frame.size.width - (searchBtn.frame.size.width + 4);

UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(2, fieldHeight+1, fieldWidth, 1)];
lineView.backgroundColor = [UIColor whiteColor];

[_MovetoSearch addSubview:lineView];

希望它对您有用

答案 2 :(得分:0)

我建议使用图层来执行此类任务,而不是过度使用UIView。

CALayer * lineLayer = [CALayer alloc]init];
[lineLayer setFrame:CGRectMake(x,y,width,1)];
[lineLayer setBackgroundColor:[UIColor whiteColor].CGColor];
[self.view.layer addSublayer:lineLayer];