Swift: Floating Plus button over Tableview using The StoryBoard

后端 未结 5 1680
忘掉有多难
忘掉有多难 2020-12-28 08:18

How to Add floating button as Facebook\'s App Rooms using the storyboard?

I can\'t drag UIView on the tableview that\'s the only way I know.

5条回答
  •  青春惊慌失措
    2020-12-28 08:56

    Its a bit late, but may be it can help someone. You can do it very easily via programatically. Just create an instance of the button, set the desired property of button It is preferable to use constraints over frame cause it'll render the button at same position on every screen size. I am posting the code which will make the button round. Play with the value of constraints to change the position and size of button.

    Swift 3

    var roundButton = UIButton()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.roundButton = UIButton(type: .custom)
        self.roundButton.setTitleColor(UIColor.orange, for: .normal)
        self.roundButton.addTarget(self, action: #selector(ButtonClick(_:)), for: UIControlEvents.touchUpInside)
        self.view.addSubview(roundButton)
    }
    
    override func viewWillLayoutSubviews() {
    
        roundButton.layer.cornerRadius = roundButton.layer.frame.size.width/2
        roundButton.backgroundColor = UIColor.lightGray
        roundButton.clipsToBounds = true
        roundButton.setImage(UIImage(named:"your-image"), for: .normal)
        roundButton.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            roundButton.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -3),  
        roundButton.bottomAnchor.constraint  
        (equalTo: self.view.bottomAnchor, constant: -53), 
        roundButton.widthAnchor.constraint(equalToConstant: 50),
        roundButton.heightAnchor.constraint(equalToConstant: 50)])
    }
    
    /** Action Handler for button **/
    
    @IBAction func ButtonClick(_ sender: UIButton){
    
     /** Do whatever you wanna do on button click**/
    
    }
    

提交回复
热议问题