SwiftUI - Add Border to One Edge of an Image

前端 未结 3 734
说谎
说谎 2021-02-04 09:07

It\'s a pretty straight-forward question - how does one apply a border effect to only the wanted edges of an Image with SwiftUI?

For example, I only want to apply a borde

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-04 09:29

    If somebody ever needs to just add a quick 1 (or more) sided border to a view (e.g., the top edge, or any random combination of edges), I've found this works well and is tweakable:

    top edge:

    .overlay(Rectangle().frame(width: nil, height: 1, alignment: .top).foregroundColor(Color.gray), alignment: .top)
    

    leading edge:

    .overlay(Rectangle().frame(width: 1, height: nil, alignment: .leading).foregroundColor(Color.gray), alignment: .leading)
    

    etc.

    Just tweak the height, width, and edge to produce the combination of borders you want.

提交回复
热议问题