简体   繁体   中英

UITableViewController.UIView.trailingAnchor has different position than UIViewController.UIView.trailingAnchor

I have a method to add two circles to the background view. When added to the view of a UIViewController , they get added correctly. However, when added to the view of a UITableViewController , they get added to the other side.

The method to create the circles is:

extension UIViewController {
    func makeCircle(radius: CGFloat) -> UIView {
         let circle = UIView()
         circle.backgroundColor = .yellow
         circle.translatesAutoresizingMaskIntoConstraints = false
         circle.layer.cornerRadius = radius
         NSLayoutConstraint.activate([
             circle.widthAnchor.constraint(equalToConstant: radius * 2),
             circle.heightAnchor.constraint(equalToConstant: radius * 2)])
         return circle
    }
}

The method to add the circles within the UIViewController extension is:

func addCircles() {
    // Center right circle
    let circle1Radius = CGFloat(152)
    let circle1 = makeCircle(radius: circle1Radius)
    view.addSubview(circle1)
    NSLayoutConstraint.activate([
        circle1.centerYAnchor.constraint(equalTo: view.centerYAnchor),
        circle1.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: circle1Radius)])

    // Bottom left circle
    let circle2Radius = CGFloat(77)
    let circle2 = makeCircle(radius: circle2Radius)
    view.addSubview(circle2)

    NSLayoutConstraint.activate([
        circle2.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 70),
        circle2.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: -50)])
}

For both classes, the circles get added inside the viewDidLoad method:

 class ViewController: UIViewController {
      override func viewDidLoad() {
          super.viewDidLoad()
          addCircles()
      }
 }

The ViewController: UIViewController , the view looks correct:

在此处输入图片说明

However, for the TableViewController: UITableViewController (exact same viewDidLoad as for controller above), the output looks opposite:

在此处输入图片说明

Can someone give me a tip on what I am doing wrong?

Thanks to @matt's comment:

I assume it's because a table view is a scroll view. Remember, constraints from a scroll view's subview to a scroll view have a completely different meaning from constraints from a normal view's subview to its superview. So adding subviews directly to a table view using constraints is going to work oddly.

What solved my issue was to no longer use a UITableViewController , but a "basic" UIViewController that has a UITableView and implements the UITableViewDataSource and the UITableViewDelegate :

class TableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        addCircles()
    }
}

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