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

时间:2015-12-06 15:40:11

标签: ios swift interface-builder

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

no shadow

它们被添加到IB中但在viewDidLoad中我绕过角落并更新背景颜色。

今天我在同一个视图上的textField中添加了一个功能。现在当VC加载/显示视图时,在四分之一秒延迟后,阴影出现在按钮后面:

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,转角舍入函数以及文本字段和文本视图的代码:

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()
    }
}

最后注意:深灰色背景是视觉效果视图。不确定这是否重要。

有人有什么想法吗?

1 个答案:

答案 0 :(得分:0)

在彻底宣布我不想以任何方式想要影子之后:

//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
}

我终于决定问问题&#34;如果我告诉它不要出现阴影,当我告诉它出现时会发生什么?&#34;我将代码更改为:

//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或以上而不是一个清晰的颜色是似乎使阴影消失的代码。如果你有任何想法,请分享它们。