A housing density law that took two years to craft was approved by the San Francisco Board of Supervisors on Tuesday, notching a win for developers and pro-growth advocates who say the only way to make San Francisco affordable is to keep building.

The ordinance, which allows developers to build taller residential structures in exchange for making 30 percent of the units affordable, passed 10-1, with Supervisor Norman Yee dissenting. Yee had asked for a special exemption for the Ocean Avenue corridor in his district, but failed to get support from the majority of his colleagues.

Supervisor Katy Tang first proposed the city density law in 2015 as a means to produce more homes for middle-class families who don’t qualify for federally subsidized housing. Her initial version died under opposition from her progressive colleagues, who said it didn’t demand enough of developers, and from westside homeowners, who said it would cause new real estate projects to flood their neighborhoods.

But Tang managed to revive the law this year, amid increased interest in helping the teachers, firefighters, nonprofit workers, small business owners and other people finding it harder and harder to squeeze by in a city with escalating rents and a rapidly changing labor market.

Some of them showed up to speak during a rally held outside City Hall on Tuesday to drum up support for the law, known as Home-SF.

“What I’m finding is that fewer and fewer police officers can afford homes,” said Tracy McCray, a sergeant at Bayview Police Station who grew up in a public housing project in the Western Addition, at the rally.

McCray bought a two-bedroom house west of Twin Peaks in 2001 and considers herself lucky. She said many of her peers wind up renting rooms or bunking with roommates once they graduate from the Police Academy. Others commute from as far away as the San Joaquin Valley.

Supervisor Ahsha Safai, who co-sponsored the legislation, called it a critical form of government intervention to keep working- and middle-class residents in the city.

Supervisor Hillary Ronen, who was considered a swing vote but wound up supporting the law, unsuccessfully pushed for an amendment that would have prevented developers from evicting the legacy businesses that give San Francisco its character.

“I don’t want cookie-cutter neighborhoods,” Ronen said.

Her colleagues shot the amendment down, fearing they would scare off developers by adding too many protections. Tang pointed out that most businesses in the city would benefit from denser housing, regardless of how old they are.

“Some corridors such as Taraval (Street) struggle,” Tang said. “When you have more density, more people can walk to the Laundromat or coffee shops. And that helps all the small businesses we’re not thinking about.”

The supervisors on Tuesday also unanimously approved a settlement with short-term rental services Airbnb and HomeAway. The companies must now ensure that all of their local hosts are registered with the city.

Rachel Swan is a San Francisco Chronicle staff writer. Email: rswan@sfchronicle.com Twitter: @rachelswan