ios 如何创建 0.5 像素的线宽

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21552115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:20:41  来源:igfitidea点击:

How to create a line width of 0.5 pixels

iosiphoneobjective-cuitableview

提问by Eyal

In iOS 7, the table view's separator line is thinner than previous iOS, and it looks like its 0.5 px width.

在 iOS 7 中,table view 的分隔线比以前的 iOS 更细,看起来像它的 0.5 px 宽度。

I need to split a cell in 2, with a similar separator line (only vertically) and I want this line to be the same width as the regular separator line.

我需要用类似的分隔线(仅垂直)将一个单元格分成 2 个单元格,并且我希望这条线与常规分隔线的宽度相同。

So I'm wondering what is the best way to add such a line?

所以我想知道添加这样一行的最佳方法是什么?

If I use a UIViewand set its width to 0.5 it won't be visible, and if I set its width to 1.0, then of course I will get a line width of 1.0px not 0.5px.

如果我使用 aUIView并将其宽度设置为 0.5,它将不可见,如果我将其宽度设置为 1.0,那么我当然会得到 1.0px 而不是 0.5px 的线宽。

I tried to use a resource with a retina size of 1.0px but it didn't work either

我尝试使用视网膜大小为 1.0px 的资源,但它也不起作用

回答by SomeGuy

You set the width as 0.5 - or to make it always 1 pixel then set it to (1.0 / [UIScreen mainScreen].scale)

您将宽度设置为 0.5 - 或者使其始终为 1 像素,然后将其设置为 (1.0 / [UIScreen mainScreen].scale)

I've done this before and it's worked fine

我以前做过这个,效果很好

I think the reason you can't see it is that you are centering the line exactly in the middle of the screen, which will make the line's origin (320 - 0.5) / 2.0, being an X origin of 159.75 - the 0.75 may be messing things up - or the cell is removing the background color of your view

我认为您看不到它的原因是您将线正好在屏幕中间居中,这将使线的原点 (320 - 0.5) / 2.0 成为 159.75 的 X 原点 - 0.75 可能是搞砸了 - 或者单元格正在删除视图的背景颜色

回答by anthonyqz

I did this steps:

我做了以下步骤:

  1. Create a constraint with 1px height
  2. Connect the constraint with IBOutlet (file .m)
  3. Change the constant of the constraint for 0.5 in the ViewDidLoad
  1. 创建一个高度为 1px 的约束
  2. 将约束与 IBOutlet 连接(文件 .m)
  3. 在 ViewDidLoad 中将约束的常量更改为 0.5

It works for me.

这个对我有用。

回答by Irina Anastasiu

Assuming you do your cell layouts with Storyboards:

假设您使用 Storyboards 进行单元格布局:

Place an UIView with your desired height and 1px width into your cell. Give it whatever background color you'll like it to have. The standard separator color is approx. RGB(200,199,204).

将具有所需高度和 1px 宽度的 UIView 放入单元格中。给它任何你喜欢的背景颜色。标准分隔符颜色约为。RGB(200,199,204)。

Then create a subclass of UIView, e.g. HairlineViewand override awakeFromNib()

然后创建一个 UIView 的子类,例如HairlineView并覆盖awakeFromNib()

-(void)awakeFromNib {
    self.layer.borderColor = [self.backgroundColor CGColor];
    self.layer.borderWidth = (1.0 / [UIScreen mainScreen].scale) / 2;

    self.backgroundColor = [UIColor clearColor];
}

You're drawing a border around your 1px wide view, which is 2 * 0.25px wide making it 0.5px in total, which is the width you wanted to achieve, then hide the background of the view by setting the background to clear. This last step is important, it won't work without.

您在 1px 宽的视图周围绘制边框,宽度为 2 * 0.25px,总共为 0.5px,这是您想要实现的宽度,然后通过将背景设置为清除来隐藏视图的背景。这最后一步很重要,没有它就行不通。

Set the type of your view to HairlineView, then it should work.

将您的视图类型设置为 HairlineView,然后它应该可以工作。

When doing it programmatically, you'd have to override a different method, e.g. initWithFrame()maybe, since awakeFromNib() only gets called when you create the view from a Storyboard layout.

以编程方式执行此操作时,您必须覆盖不同的方法,例如initWithFrame()可能,因为仅当您从 Storyboard 布局创建视图时才调用awakeFromNib()。

回答by kientux

Some of the answers above are already right, but just an update: in Xcode 8, now you can set constant of layout constraint with a decimal number. No longer need to connect IBOutlet or set constant via User Defined Runtime Attributes.

上面的一些答案已经是正确的,但只是一个更新:在 Xcode 8 中,现在您可以使用十进制数设置布局约束常量。不再需要通过用户定义的运行时属性连接 IBOutlet 或设置常量。

Decimal constant

十进制常数

回答by Andrey Dyatkov

You can set constant of layout constraint with a decimal number, but UIBuilder have a some bug in Xcode 8,9,10

您可以使用十进制数设置布局约束的常量,但是 UIBuilder 在 Xcode 8,9,10 中有一些错误

My solution:

我的解决方案:

  1. Open xib file by source code enter image description here]
  2. Find your constant of layout constraint and change to 0.5 enter image description here
  1. 通过源代码打开xib文件在此处输入图片说明]
  2. 找到您的布局约束常数并更改为 0.5 在此处输入图片说明

回答by Rabiea

you can set constraint in storyboard height 0.5f; it won't be visible directly however it is visible under list of of constraints as shown in screenshot enter image description here

您可以在故事板中设置约束height 0.5f;它不会直接可见,但它在约束列表下可见,如屏幕截图所示在此处输入图片说明

回答by Rabiea

You can use Core Graphicsto draw the gradient:

您可以使用Core Graphics绘制渐变:

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 1.0f)];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = view.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor], (id)[[UIColor blackColor] CGColor], nil];
[view.layer insertSublayer:gradient atIndex:0];

回答by bubuStar

my solution:

我的解决方案:

  1. create a view, set a constraint the view: "height = 0px", go to the "Identity Inspector" and edit the priority of this constraint to 750.

  2. then select the view and click the "pin" button, add another constraint to this view: "height = 0.5" with priority of 1000. in the "Identity Inspector" you can see the second constraint automatically becomes "height >= 0.5"

  1. 创建一个视图,在视图中设置约束:“height = 0px”,转到“身份检查器”并将此约束的优先级编辑为 750。

  2. 然后选择视图并单击“pin”按钮,向该视图添加另一个约束:“height = 0.5”,优先级为 1000。在“Identity Inspector”中,您可以看到第二个约束自动变为“height >= 0.5”

works in Xcode 7.3.1

适用于 Xcode 7.3.1

回答by mishimay

Tricky way:

棘手的方法:

  1. In storyboard or xib, set a unique value (e.g. 123) to the line height constraint
  2. Open the storyboard or xib with text editor
  3. Replace the unique value (e.g. 123) with 0.5
  1. 在 storyboard 或 xib 中,为行高约束设置一个唯一值(例如 123)
  2. 使用文本编辑器打开故事板或 xib
  3. 用 0.5 替换唯一值(例如 123)

回答by Kirit Modi

You Put view in you cell at the bottom. and set its to dark color of the view. and also set its height 0.5 PX. you can got separator of each cell in it.keep its width according to cell width.

您将视图放在底部的单元格中。并将其设置为视图的深色。并将其高度设置为 0.5 PX。您可以在其中获得每个单元格的分隔符。根据单元格宽度保持其宽度。