Do sunscreens prevent you getting necessary vitamin D?

No. Only a small amount of vitamin D is received directly from diet. The majority of our bodies vitamin D comes from conversion in our skin due to sunlight exposure. We know vitamin D is critical for healthy bones but it may have other major health benefits in many areas of our body.

So, if we protect ourselves against skin cancer with sunscreen - are we risking getting low on vitamin D? Fortunately, a number of studies demonstrate that sunscreen, in normal use, does not cause vitamin D deficiency. There are a number of explanations for this. Firstly, sunscreen is only a screen, not a total block. Some UV still gets through to the skin. Secondly, the data tells us, we only need a very small amount of UV to generate vitamin D. Depending on your skin type, exposing just 10% of your skin to 3-7 mins of sun in the New Zealand summer is enough to provide adequate vitamin D without sunburn. In fact, after a certain point of exposure, vitamin D begins to get degraded by further UV exposure. Finally, when sunscreen is applied in real-world situations, it not applied as thickly, as completely, or as frequently as it should be to provide maximum protection.

Sources: Research Review Educational Series. An update on sunscreen IV. 2015 and Sunscreen Myths Busted. 
Medscape. Jul 19, 2016.