xcode 如何在swift 4中像iOS appstore一样应用卡片视图cornerRadius和shadow

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51095450/
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-09-15 10:41:30  来源:igfitidea点击:

How to Apply card view cornerRadius & shadow like iOS appstore in swift 4

xcodeswift3app-storeios11

提问by Rahul Singha Roy

I want to apply "cornerRadius" and card view "shadow" in my collection view cell like iOS appstore today View.

我想在我的收藏视图单元格中应用“ cornerRadius”和卡片视图“ shadow”,例如今天的 iOS appstore View。

CardView shadow example 1

CardView 阴影示例 1

iOS appstore today view

iOS 应用商店今日视图

回答by oyvindhauge

Just add a subview to the cell and manipulate it's layer property. Tweak the values to your liking. The following code should give a similar result to how it looks in the App Store:

只需向单元格添加一个子视图并操作它的图层属性。根据自己的喜好调整值。下面的代码应该给出与它在 App Store 中的外观类似的结果:

    // The subview inside the collection view cell
    myView.layer.cornerRadius = 20.0
    myView.layer.shadowColor = UIColor.gray.cgColor
    myView.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
    myView.layer.shadowRadius = 12.0
    myView.layer.shadowOpacity = 0.7

enter image description here

在此处输入图片说明

回答by Zahidur Rahman Faisal

Create a new UIView subclass named "CardView" like below:

创建一个名为“CardView”的新 UIView 子类,如下所示:

import Foundation
import UIKit

@IBDesignable
class CardView: UIView {

    @IBInspectable var cornerRadius: CGFloat {
        get {
            return layer.cornerRadius
        }
        set {
            layer.cornerRadius = newValue
            layer.shadowRadius = newValue
            layer.masksToBounds = false
        }
    }

    @IBInspectable var shadowOpacity: Float {
        get {
            return layer.shadowOpacity
        }
        set {
            layer.shadowOpacity = newValue
            layer.shadowColor = UIColor.darkGray.cgColor
        }
    }

    @IBInspectable var shadowOffset: CGSize {
        get {
            return layer.shadowOffset
        }
        set {
            layer.shadowOffset = newValue
            layer.shadowColor = UIColor.black.cgColor
            layer.masksToBounds = false
        }
    }

}

Then just set "CardView" as Custom Class for your view from XCode Interface Builder. It's simple and easily configurable!

然后只需将“CardView”设置为来自 XCode Interface Builder 的视图的自定义类。它简单且易于配置!

回答by Mojtaba Hosseini

- SwiftUI

- SwiftUI

struct SimpleRedView: View {
    var body: some View {
        Rectangle()
            .foregroundColor(.red)
            .frame(width: 340, height: 500, alignment: .center)
    }
}

struct ContentView: View {
    var body: some View {
        SimpleRedView()
        .cornerRadius(28)
        .shadow(radius: 16, y: 16)
    }
}

SimpleRedView()is just for placeholder and you can replace it with any kind of Viewyou like.

SimpleRedView()仅用于占位符,您可以将其替换为View您喜欢的任何类型。

回答by Dominic Holmes

To add onto @oyvindhauge 's great answer -- make sure none of the subviews inside myViewextend to the edges. For instance, my card view contained a table view that fills the card -- so it's necessary to set the tableView.layer.cornerRadius = 20.0as well. This applies to any subview that fills the card.

添加到@oyvindhauge 的好答案 - 确保里面的子视图都没有myView延伸到边缘。例如,我的卡片视图包含一个填充卡片的表格视图——所以也有必要设置它tableView.layer.cornerRadius = 20.0。这适用于填充卡片的任何子视图。