iamrkasula
iamrkasula

Reputation: 274

UIView Width issue

I've got two table views of same height and width in UIView. I'm trying to place a divider(UIView of 1px width) in between tableviews as shown in picture but it is looking like 2px wide. i have tried placing the divider within .xib and programmatically as well but I'm getting the same issue. I want to achieve a divider with thickness same as a tableview cell divider line thickness.

UIView *vLine = [[UIView alloc]initWithFrame:CGRectMake(300,0,1,500)];
vLine.backgroundColor = [UIColor colorWithRed:207.0f/255.0f green:207.0f/255.0f blue:207.0f/255.0f alpha:1.0];
[self.view addSubview:vLine];

enter image description here

Upvotes: 2

Views: 135

Answers (4)

MrJre
MrJre

Reputation: 7161

Try this, as its more robust:

CGFloat screenScale = [[UIScreen mainScreen] scale];
UIView *vLine = [[UIView alloc] initWithFrame:CGRectMake(300, 0, 1 / screenScale, 500)];

Upvotes: 2

Sudhin Davis
Sudhin Davis

Reputation: 2020

Try setting pattern image with 1px width containing 0.5 px clear color and other 0.5px with your separator color.

An example pattern image is given below.

 UIView *vLine = [[UIView alloc]initWithFrame:CGRectMake(300,0,1,500)];
    vLine.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"myPatternImage.png"]];

Here your pattern image should be of 1px width containing 0.5 px clear color and other 0.5px with your separator color in normal screen. Try ith with the image provided below.[ enter image description here ]

Upvotes: 0

Ben Resplendent
Ben Resplendent

Reputation: 408

The height of the native table view separator is 0.5 pixels. Simply adjust your frame to:

UIView *vLine = [[UIView alloc]initWithFrame:CGRectMake(300.0f,0.0f,0.5f,500.0f)];

Upvotes: -1

Shashank
Shashank

Reputation: 1753

For a retina device, 1 point will be 2px. Hence, use 0.5 as width for retina devices and 1 for non retina devices.

if(is_retina) {
     [[UIView alloc]initWithFrame:CGRectMake(300,0,0.5,500)];
} else {
     [[UIView alloc]initWithFrame:CGRectMake(300,0,1,500)]    
}

Upvotes: 1

Related Questions