ios - 如何删除不需要的按钮阴影

标签 ios swift interface-builder

我的 iOS 应用程序已经有这两个按钮几个月了:

no shadow

它们是在 IB 中添加的,但在 viewDidLoad 中我圆角并更新背景颜色。

今天我向同一 View 上的文本字段添加了一项功能。现在,当 VC 加载/呈现 View 时,会出现 View ,并在四分之一秒的延迟后,按钮后面会出现阴影:

shadow

我没有更改属性检查器中的任何内容。我也没有更改按钮周围的代码。自从问题开始以来,我现在添加了显式的无阴影代码,并确保检查器设置为透明阴影。

func roundBut (Type: UIView) {
    Type.layer.cornerRadius = 6
    Type.layer.borderWidth = 0.0
    Type.layer.masksToBounds = true
    Type.layer.borderColor = UIColor.whiteColor().CGColor
    Type.layer.shadowOpacity = 0.0
    Type.layer.shadowColor = nil
    Type.layer.shadowRadius = 0.0
}

最后一个奇怪的事情是,如果我单击文本字段,阴影就会消失。

根据要求提供更多代码:

这里几乎是与此相关的所有代码,包括 viewDidLoad、圆角函数以及文本字段和 TextView 的代码:

override func viewDidLoad() {
    super.viewDidLoad()

    //If user is editing a cell, check...
    if passingEdit == true {
        //If passed an 'Add New' cell: It clears that text
        if namePassed == nil && descPassed == "Add new" {
            txtDesc.text = ""
            addSave.setTitle("Add", forState: UIControlState.Normal)
            action.hidden = true
            actionsBox.hidden = true
            passingEdit = false
        }
        else {
            //Otherwise: Fill both fields with cell's content
            txtTask.text = namePassed
            txtDesc.text = descPassed
            action.hidden = true //Becasue action box will be visible
            addSave.setTitle("Update", forState: UIControlState.Normal)
        }
    }
    else {
        txtTask.becomeFirstResponder()
        addSave.setTitle("Add", forState: UIControlState.Normal)
        action.hidden = true
        actionsBox.hidden = true
    }

    //If user hasn't added details: Show Placeholder
    if txtDesc.text.isEmpty {
        txtDesc.text = "Tap here to add details."
        txtDesc.textColor = UIColor.lightGrayColor()
    }

    //Sets editor header text
    switch (currentListEntity) {
    case "TodayTask":
        hdr_Txt.setTitle("Today", forState: UIControlState.Normal)
        txtTask.attributedPlaceholder = NSAttributedString(string:"I need to...", attributes:[NSForegroundColorAttributeName: UIColor.lightGrayColor()])
        move_TopBtn.setTitle("Add to 'Tomorrow'", forState: UIControlState.Normal)
        move_TopBtn.backgroundColor = UIColor(red: 49/255.0, green: 82/255.0, blue: 172/255.0, alpha: 1.0)
    case "TomTask":
        hdr_Txt.setTitle("Tomorrow", forState: UIControlState.Normal)
        txtTask.attributedPlaceholder = NSAttributedString(string:"I need to...", attributes:[NSForegroundColorAttributeName: UIColor.lightGrayColor()])
        move_TopBtn.setTitle("Add to 'Today'", forState: UIControlState.Normal)
        move_TopBtn.backgroundColor = UIColor(red: 0/255.0, green: 128/255.0, blue: 0/255.0, alpha: 1.0)
    case "TBDTask":
        hdr_Txt.setTitle("Do Later", forState: UIControlState.Normal)
        txtTask.attributedPlaceholder = NSAttributedString(string:"At some point, I need to...", attributes:[NSForegroundColorAttributeName: UIColor.lightGrayColor()])
    case "FinTask":
        //This won't likely run, as "Fin" does not have an add button
        hdr_Txt.setTitle("Done", forState: UIControlState.Normal)
    default:
        //Should run when user's current location is the Life Lists
        hdr_Txt.setTitle("List Item", forState: UIControlState.Normal)
        txtTask.attributedPlaceholder = NSAttributedString(string:"Name of List Item", attributes:[NSForegroundColorAttributeName: UIColor.lightGrayColor()])
        move_TopBtn.setTitle("Add to 'Today'", forState: UIControlState.Normal)
        move_TopBtn.backgroundColor = UIColor(red: 0/255.0, green: 128/255.0, blue: 0/255.0, alpha: 1.0)
        move_BotBtn.setTitle("Add to 'Tomorrow'", forState: UIControlState.Normal)
        move_BotBtn.backgroundColor = UIColor(red: 49/255.0, green: 82/255.0, blue: 172/255.0, alpha: 1.0)
    }


    //Round check / delete views
    makeCircle(deleteBtn)
    makeCircle(doneBtn)
    //Round corners of rectangles
    roundBut(move_TopBtn)
    roundBut(move_BotBtn)

    move_TopBtn.layer.shadowColor = UIColor.clearColor().CGColor
    move_TopBtn.layer.shadowRadius = 0.0

    print("editorVC viewDidLoad ran")
}//End of viewDidLoad




//*****************************
//Setup - Reference Functions
//*****************************

//Round contentView
func roundBut (Type: UIView) {
    Type.layer.cornerRadius = 6
    Type.layer.borderWidth = 0.0
    Type.layer.masksToBounds = true
    Type.layer.borderColor = UIColor.whiteColor().CGColor
    Type.layer.shadowOpacity = 0.0
    Type.layer.shadowColor = nil
    Type.layer.shadowRadius = 0.0
}
func makeCircle (viewToRound: UIButton) {
    viewToRound.layer.cornerRadius = viewToRound.frame.size.width/2
    viewToRound.layer.borderWidth = 0.0
    viewToRound.layer.masksToBounds = true
}



//***** ----- ***** ------ ***** ----- ***** ----- *****
//Functionality
//***** ----- ***** ------ ***** ----- ***** ----- *****

func textFieldDidBeginEditing(textField: UITextField) {
    if txtTask.text != "" {
        action.hidden = false
    }
}

//Dismisses keyboard upon tapping the return key
func textFieldShouldReturn(textField: UITextField) -> Bool{
    textField.resignFirstResponder()
    return true
}

//Dismisses keyboard upon touch outside text boxes
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    self.view.endEditing(true)
    action.hidden = true
}

func textViewDidBeginEditing (textView: UITextView) {
    if textView.textColor == UIColor.lightGrayColor() {
        textView.text = nil
        textView.textColor = UIColor.whiteColor()
    }
    action.hidden = false
}

func textViewDidEndEditing(textView: UITextView) {
    if textView.text.isEmpty {
        txtDesc.text = "Tap here to add details."
        textView.textColor = UIColor.lightGrayColor()
    }
}

最后说明:深灰色背景是视觉效果 View 。不确定这是否重要。

大家有什么想法吗?

最佳答案

在彻底声明我不想要尽我所能的阴影之后:

//Rounded Rectangles
func roundBut (Type: UIView) {
    Type.layer.shadowOpacity = 0.0
    Type.layer.shadowOffset = CGSizeMake(0, 0)
    Type.layer.shadowColor = UIColor.clearColor().CGColor
    Type.layer.shadowRadius = 0.0
    Type.layer.shadowOpacity = 0.0
    Type.layer.cornerRadius = 12.0
    Type.layer.masksToBounds = false
    Type.layer.borderWidth = 0.0
}

我最终决定问这个问题:“如果当我告诉它不要出现时,阴影就出现了,那么当我告诉它出现时会发生什么?”我将代码更改为:

//Rounded Rectangles
func roundBut (Type: UIView) {
    Type.layer.shadowOpacity = 1.0
    Type.layer.shadowOffset = CGSizeMake(500, 500)
    Type.layer.shadowColor = UIColor.clearColor().CGColor
    Type.layer.shadowRadius = 150.0
    Type.layer.cornerRadius = 12.0
    Type.layer.masksToBounds = false
    Type.layer.borderWidth = 1.0
    Type.layer.borderColor = UIColor(red: 127/255.0, green: 127/255.0, blue: 127/255.0, alpha: 0.10).CGColor
}

至少现在已经解决了。我必须密切关注它,以防出现问题并得到修复。

我不知道为什么这解决了我的问题。摆弄它,边框为 1.0 或以上,并且不是清晰的颜色,这似乎是使阴影消失的代码。如果您有任何想法,请分享。

关于ios - 如何删除不需要的按钮阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34119232/

相关文章:

iphone - Points对UIImage的透视校正

ios - 键值观察(KVO)与绑定(bind)(_ :to:withKeyPath:options:)

ios - swift 类中的 AWS 配置行导致线程 1 : EXC_BAD_INSTRUCTION

ios - 默认 TabBar 应用程序

fonts - 是否可以在 Interface Builder 中使用 SF Rounded

ios - OptionSet 中是否可以有超过 64 个成员?

ios - 将zip写入cacheDirectory时,iOS NSData.write(toFile)返回false

ios - 是否可以从 wkwebview 中经过身份验证的 session 下载 pdf?

ios - Swift - UIViewController 内的 UITableView,不调用 UITableView 函数

interface-builder - 在 Xcode 6 beta 5 中,将自动布局约束设置为占位符似乎不起作用?