An overlooked part of Spain's problem is that investing in having a very big apartment is a much better savings vehicle than investing in stock markets. The equivalent of an American 401k has a very small contribution limit, and wealth taxes hit pretty hard and pretty early. So as family sizes shrink, there's a lot of housing overconsumption. A single person, or a family of 2, living in what in the 80s was a family of 6, plus a live-in nanny. So for the same population, you need far more built space. The property tax for owning a pretty expensive apartment are very low by global standards, and housing appreciation isn't a real thing until you sell.
My family in Spain is either very house poor, making little and having to rent, or inherited more house than they ever need, and keep it underoccupied because trying to put it to more productive use is pretty risky. Regulations that foster better utilization would make a big dent, but they would be extremely unpopular. Even those that don't own an apartment just want one to be cheap so they can use it as a wealth building tool, as almost nobody wants housing as an investment to be a pretty dubious deal.
There _are_ other ways. Less efficient and arguably more expensive, but for completeness sake they should be mentioned.
For example, build infrastructure that favor sprawling. This way people will prefer to live in nicer, smaller towns as long as they're still 30 minutes train ride from the city. (I'm in Paris now, and their RER is an awesome example of this.)
Encourage remote work, or at least don't hinder it.
Test some anti-car measures, like congestion taxes or just make whole neighborhoods car free. This makes commute time less directly proportional to physical distance - if you have to walk 10 minutes or take a train either way, you're more ok with living in a suburb.