简体   繁体   中英

add subview over UITableView

First of, I am noob to Swift programming.

I am trying to show a custom view over a UITableView.

I am facing two major issues :

a) The subview does not appear correctly firstly. ref image below :

出现在addSubview上

and after few moments actual view is loaded :

实际视图外观

b) After the UITableView is scrolled, the view is not coming over the current top scroll position of UITableView 滚动表视图后

Following is the code to add subview :

let vaAdPopupViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "AdPopupWithCrossViewController") as! AdPopupWithCrossViewController
                            vaAdPopupViewController.adID = adID
                            vaAdPopupViewController.tableView = self.tableView
                            if let message = parseJSON["adv_text"] as? String{
                                vaAdPopupViewController.message = message
                            }


                            let navigationBarFrame: CGRect = (self.navigationController?.view.frame)!
                            let frameSize = CGRect(x: navigationBarFrame.minX, y: navigationBarFrame.minY, width: navigationBarFrame.width, height: (navigationBarFrame.height - (self.navigationController?.navigationBar.frame.size.height)!))
                            vaAdPopupViewController.view.frame = frameSize
                            self.tableView.addSubview(vaAdPopupViewController.view)


                            vaAdPopupViewController.view.tag = 1000
                            self.addChildViewController(vaAdPopupViewController)
                            vaAdPopupViewController.didMove(toParentViewController: self)

also in AdPopupWithCrossViewController :

override func viewDidLoad() {
        self.view.superview?.layoutIfNeeded()
        self.view.layoutIfNeeded()
        super.viewDidLoad()
}

Please guide me how to resolve this issue.

Thank You.

As Dan says, you should add your popup as a subview of the view controller's view, not of the table view. A Table view's view hierarchy is private and you should not be trying to mess with it.

Note that you can't do this with a table view controller. This has always bugged me, but it's a fact: A UITableViewController manages a single table view and nothing else. If you want a more complex view hierarchy you need to embed your table view controller in another view controller. I do this with a container view and an embed segue, which is very easy.

If you do that then you can add your new view controller's content to the parent view controller's content view, not to the table view controller's view (or to the table view itself)

You probably want to either create a Present Modally Segue or use present() via code.

You've already created your AdPopupWithCrossViewController . Set its background color to be translucent. Then you can present it like this:

    let vaAdPopupViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "AdPopupWithCrossViewController") as! ModalOverTableViewController
    vaAdPopupViewController.modalPresentationStyle = .overCurrentContext
    self.present(vaAdPopupViewController, animated: true, completion: nil)

Then, your little "X" button can remove the ad with:

    @IBAction func closeTapped(_ sender: Any) {
        self.dismiss(animated: true, completion: nil)
    }

You can try out the various options on .modalPresentationStyle and .modalTransitionStyle , as well as setting animated to true or false, to get the appearance / behavior you desire.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM