简体   繁体   English

界面生成器中的iOS阴影视图

[英]iOS shadow view in interface builder

I'm trying to add shadows to some views via interface builder. 我正在尝试通过界面生成器向某些视图添加阴影。 I can't seem to get shadows to work on my views. 我似乎无法在我的观点上留下阴影。 All the resources I look at point to this same code so I'm not sure what I'm doing wrong. 我看到的所有资源都指向同一代码,所以我不确定自己在做什么错。

Interface Builder 界面生成器

界面生成器

Interface Builder Extension code Interface Builder扩展代码

import Foundation
import UIKit

extension UIView {

    //cut irrelevant code for SO Question

    @IBInspectable
    var masksToBounds: Bool {
        get {
            return layer.masksToBounds
        }
        set {
            layer.masksToBounds = newValue
        }
    }


    // Shadow handling
    @IBInspectable
    var shadowColor: UIColor? {
        get {
            if let color = layer.shadowColor {
                return UIColor(cgColor: color)
            }
            return nil
        }
        set {
            if let color = newValue {
                layer.shadowColor = color.cgColor
            } else {
                layer.shadowColor = nil
            }
        }
    }

    @IBInspectable
    var shadowOpacity: Float {
        get {
            return layer.opacity
        }
        set {
            layer.opacity = newValue
        }
    }


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

    @IBInspectable
    var shadowOffset: CGSize {
        get {
            return layer.shadowOffset
        }
        set {
            layer.shadowOffset = newValue
        }
    }


}

Views layout 视图布局

在此处输入图片说明

This is the result 这是结果

在此处输入图片说明

I guess it's your custom search bar to which you're adding shadow. 我想这是您要添加阴影的自定义搜索栏。 The reason its not visible I guess is because your both view in controller and storyboard of same size. 我猜它不可见的原因是因为您在控制器和情节提要中的视图均相同。 Try to make the storyboard view have a container view in it with some padding along to the storyboard view and add that shadow to that container view. 尝试使情节提要视图具有一个容器视图,并在情节提要视图中添加一些填充,然后将阴影添加到该容器视图。 This might show some results. 这可能会显示一些结果。

Your shadowOpacity property is accessing the wrong layer property. 您的shadowOpacity属性正在访问错误的layer属性。 It's accessing layer.opacity when it should be layer.shadowOpacity . 它的访问layer.opacity当它应该是layer.shadowOpacity Also, your shadowRadius is way too large and should probably be around 5 (depending on what you're going for, of course), not 500. 另外,您的shadowRadius太大了,应该大约为5(当然,这取决于您要做什么),而不是500。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS在界面生成器中使用继承的View - IOS use inherited View inside interface builder 具有自动布局的界面构建器中的iOS滚动视图未滚动 - iOS scroll view in interface builder with auto layout is not scrolling iOS和界面生成器:放错视图(例如,UITableViewCell) - iOS and interface builder: misplaced view (e.g. UITableViewCell) iOS版。 我可以在Interface Builder中为UIScrollView制作自定义视图吗? - iOS. Could I made custom view for UIScrollView in Interface Builder? 界面生成器中的iOS Swift 3.0自定义视图导致重新编译和放错位置 - iOS Swift 3.0 custom view in interface builder causes recompiling and misplacements iOS:Interface Builder中定义的颜色与Web视图中的颜色不同 - iOS: Colors defined in Interface Builder different from colors in Web View iOS-更改Interface Builder中视图的大小以放入scrollview - iOS - Change size of view in Interface Builder for putting into scrollview 界面构建器视图ui设计在横向模式下更改ios - interface builder view ui design changing in landscape mode ios iOS - Interface Builder - 在同一个xib中绑定多个视图 - iOS - Interface Builder - bind multiple view inside the same xib 基于约束的iOS Update Interface Builder视图框架 - iOS Update Interface Builder view frame based on constraints
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM