【问题标题】:How to add 3 sided border for an image in iOS如何在 iOS 中为图像添加 3 边边框
【发布时间】:2012-07-17 17:10:12
【问题描述】:

我想为图像添加边框。我不想将它完全添加到 4 个方面。我只想在特定的 3 个边上添加边框,即顶部、左侧和底部。该怎么做?

[imageView.layer setBorderColor:[[UIColor clearColor] CGColor]];

这将在图像的 4 侧设置边框。但我只希望它在 3 个方面。该怎么做?

【问题讨论】:

标签: iphone ios ios5 uiimageview border


【解决方案1】:

您需要在想要设置边框的边缘添加彩色子视图。请参阅this answer——对于顶部的边框,遵循相同的模式,但使用autoresizingMaskUIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin

【讨论】:

    【解决方案2】:

    确保 UIImageView 的 clipsToBounds 设置为 YES。创建一个边框宽度比图像宽的 UIView。将该图层上的边框设置为所需的颜色。将此视图的框架设置为 0,0、imageWidth+border、image.height 并设置 maskToBounds = YES。将此作为子视图添加到您的 UIImageView。它应该在除右侧之外的所有区域设置边框。

    【讨论】:

    • I don't want to use subviews because, I am using that imageView as thumbnails, When that particular thumb nail is selected I should highlight the combination of two images with a single border.实际上我想使用两个并排的两个图像和一个边框..
    • 那么您可以创建一个 UIView,将两个图像作为子视图添加到其中,并使用将它们并排放置的框架,使该视图更大一些(并插入图像),然后在此容器视图中设置图层边框。 2x 边框 + 图像尺寸将是容器视图的正确尺寸。
    【解决方案3】:

    您可以在实际图像层后面绘制一个高 2 像素、宽 1 像素的填充 CALayer。将框架设置为图像上方和左侧 1 像素。

    【讨论】:

      【解决方案4】:

      用阴影操作视图,这很容易做到

      _topInfoView.layer.masksToBounds = NO;
      _topInfoView.layer.shadowOffset = CGSizeMake(0, 1);
      _topInfoView.layer.shadowColor = [[UIColor grayColor]CGColor];
      _topInfoView.layer.shadowRadius = 0.27f;
      _topInfoView.layer.shadowOpacity = 0.6;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-02-27
        • 2019-04-23
        • 1970-01-01
        • 2013-01-22
        • 1970-01-01
        • 2021-05-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多