A mean field theory of the effect of knots on the statistical mechanics of ring polymers is presented. We introduce a topological invariant which is related to the primitive path in the "polymer in the lattice of obstacles" model and use it to estimate the entropic contribution to the free energy of a nonphantom ring polymer. The theory predicts that the volume of the maximally knotted ring polymer is independent of solvent quality and that the presence of knots suppresses both the swelling of the ring in a good solvent and its collapse in a poor solvent. The probability distribution of the degree of knotting is estimated and it is shown that the most probable degree of knotting upon random closure ofthe chain grows dramatically with chain compression. The theory also predicts some unexpected phenomena such as "knot segregation" in a swollen polymer ring, when the bulk of the ring expels all the entanglements and swells freely, with all the knots concentrated in a relatively small and compact part of the polymer.