在 uiviewcontroller 的视图中绘制水平线和垂直线

发布于 2024-12-31 23:40:42 字数 92 浏览 2 评论 0原文

我有一个带有一些图像的 UIViewController 。我需要在图像之间绘制一些水平线和垂直线。实际上它就像一个层次结构视图。添加带有背景颜色的子视图是最好的方法吗?

I have a UIViewController with some images. I need to draw some horizontal and vertical lines in between images. Actually its like a hierarchy view. Is adding subviews with background color the best way to go?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

沐歌 2025-01-07 23:40:43
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UIView *verticalLine = [[UIView alloc] initWithFrame:CGRectMake(roundf(self.view.bounds.size.width / 2), 0.0, 1.0, self.view.bounds.size.height)];
    verticalLine.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.5];
    verticalLine.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin;
    [self.view addSubview:verticalLine];

    UIView *horizontalLine = [[UIView alloc] initWithFrame:CGRectMake(0.0, roundf(self.view.bounds.size.height / 2), self.view.bounds.size.width, 1.0)];
    horizontalLine.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.5];
    horizontalLine.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
    [self.view addSubview:horizontalLine];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UIView *verticalLine = [[UIView alloc] initWithFrame:CGRectMake(roundf(self.view.bounds.size.width / 2), 0.0, 1.0, self.view.bounds.size.height)];
    verticalLine.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.5];
    verticalLine.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin;
    [self.view addSubview:verticalLine];

    UIView *horizontalLine = [[UIView alloc] initWithFrame:CGRectMake(0.0, roundf(self.view.bounds.size.height / 2), self.view.bounds.size.width, 1.0)];
    horizontalLine.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.5];
    horizontalLine.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
    [self.view addSubview:horizontalLine];
}
_畞蕅 2025-01-07 23:40:43

我使用了 Magic Bullet Dave 发布的第三个选项,效果很好。代码如下:

UIView *borderBottom = [[UIView alloc] initWithFrame:CGRectMake(0.0, 10, widthDesired, 1.0)];
borderBottom.backgroundColor = [UIColor grayColor];
[myView addSubview:borderBottom];

如果你想制作一条垂直线,你只需使用宽度 1,然后使用所需的高度。

I used the 3rd option posted by Magic Bullet Dave and it is working great. Here's the code:

UIView *borderBottom = [[UIView alloc] initWithFrame:CGRectMake(0.0, 10, widthDesired, 1.0)];
borderBottom.backgroundColor = [UIColor grayColor];
[myView addSubview:borderBottom];

If you want to make a vertical line you'd simply use a width of 1 and then the height desired.

蓝咒 2025-01-07 23:40:42

您有 3 种基本方法:

  1. 使用 QuartzCore 并覆盖 drawRect: 在自定义 UIView 子类中
  2. 设置包含每个图像的 UIImageView 图层属性的 borderWidth 和 borderColor
  3. 创建水平线高度为 1 、垂直线宽度为 1 的 UIView,设置背景颜色视图并将它们添加为子视图

3 可能是最容易实现的,但不是最优雅的,1 就内存而言是最强大的,因为您还可以使用 drawInRect 将图像绘制到相同的图形中 语境。这会将视图层次结构折叠为单个视图。

You have 3 basic approaches:

  1. Use QuartzCore and override drawRect: in a custom UIView subclass
  2. Set the borderWidth and borderColor of the UIImageView layer property that contains each image
  3. Create UIViews of height 1 for horizontal lines and width 1 for vertical lines, set the backgroundColor of the views and add them as subviews

3 is probably the easiest to implement, but not the most elegant, 1 is the most robust in terms of memory as you can also use drawInRect to draw your images into the same graphics context. This collapses the view hierarchy into a single view.

知足的幸福 2025-01-07 23:40:42

您可以使用上面回答的图层来完成此操作,或者只是因为您只想要线条,所以使用 UIViews

就像这样

for(i=0;i<numberOfLine*heightofImage;i+=heightOfImage) {
    UIView *horizontalLine=[[UIView alloc]initWithFrame:CGRectMake(x, i, height, 1)];
    [self.view addSubView:horizontalLine];
}

希望有帮助

You can do it using layers as answered above or simply since you want just lines,use UIViews

Just like this

for(i=0;i<numberOfLine*heightofImage;i+=heightOfImage) {
    UIView *horizontalLine=[[UIView alloc]initWithFrame:CGRectMake(x, i, height, 1)];
    [self.view addSubView:horizontalLine];
}

Hope that helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文