【问题标题】:Swift change backgroundcolor and have rounded corners in an UIVIew?快速更改背景颜色并在 UIVIew 中有圆角?
【发布时间】:2021-12-15 11:57:48
【问题描述】:

我在更改背景颜色和同时制作圆角时遇到问题。

droppedView.roundCorners(corners: .bottomLeft, radius: 7)
droppedView.roundCorners(corners: .bottomRight, radius: 7)
droppedView.backgroundColor = .systemGray6

当我这样做时,我的视图有圆角,但没有背景色。

这个问题有解决办法吗?

【问题讨论】:

  • 您要应用修饰符的视图是视图还是 UI 形状?在这种情况下使用.fill() 然后.roundCorners()
  • 'roundCorners' 从何而来?
  • UIKit 添加到您的标签大军中,以免误导他人。
  • 感谢您的帮助,但不幸的是它还没有奏效。
  • 是的,我正在尝试将其应用于 UIView

标签: swift uiview uikit


【解决方案1】:

你可以使用.clipShape:

droppedView.background(Color.systemGray6)
droppedView.clipShape(Rectangle().roundCorners(corners: [.bottomLeft, .bottomRight], radius: 7))

【讨论】:

  • 什么是矩形()?使用 UIKit 的 clipShape?
【解决方案2】:

UIKit

你可以在 UIKit 中使用layer.cornerRadius

    droppedView.clipsToBounds = true
    droppedView.layer.cornerRadius = 7

    droppedView.layer.maskedCorners = [.layerMaxXMaxYCorner, .layerMinXMaxYCorner]
    droppedView.backgroundColor = .systemGray6

SwiftUI

clip.Shape 在 SwiftUI 中可用。

首先,创建File View+Extensions.swift:

import SwiftUI

extension View {
    func cornerRadius(_ radius: CGFloat, corners: UIRectCorner) -> some View {
        clipShape( RoundedCorner(radius: radius, corners: corners))
    }
}

struct RoundedCorner: Shape {

    var radius: CGFloat = .infinity
    var corners: UIRectCorner = .allCorners

    func path(in rect: CGRect) -> Path {
        let path = UIBezierPath(roundedRect: rect, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        return Path(path.cgPath)
    }
}

然后,转到View.Swift

Rectangle().cornerRadius(50, corners: .bottomRight)
Rectangle().cornerRadius(50, corners: .bottomLeft)

【讨论】:

    【解决方案3】:

    试试这个可能对你有帮助的解决方案

            droppedView.clipsToBounds = true
            droppedView.layer.cornerRadius = 7
            droppedView.layer.maskedCorners = [.layerMaxXMaxYCorner, .layerMinXMaxYCorner]
            droppedView.backgroundColor = .systemGray6
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-25
      • 2011-12-30
      • 1970-01-01
      相关资源
      最近更新 更多