Where I currently live developers are required by law to build. Certain percentage of square meters of social housing for every square meter of higher class housing they build. When my house got build they had to build social housing right next to it.
If I understood it correctly, the city ties this to the building permits. So if you wanna build new stuff you need to show that your plans include these social and affordable flats as well.
A city should in my opinion regulate to maximize the happieness of it’s inhabitants and not it’s developers (to some degree these two overlap anyways).
Certainly you could also try to change things via the market, but unless your city has a lack of developers why should you?