SYDNEY — Australia’s deputy prime minister resigned Friday after being undermined by a sex scandal that triggered a ban on sex between government ministers and their personal staff and a national debate over exposing politicians’ bedroom behavior.

Barnaby Joyce, the leader of the rural-based Nationals Party, told reporters that a sexual harassment complaint against him that emerged in the media the previous day had convinced him that his position was politically untenable.

The complaint added to the pressure from the revelation three weeks ago that Joyce and his former press secretary had had an extramarital relationship and are expecting a baby.

A ruddy-faced political maverick who once threatened to euthanize actor Johnny Depp’s pet dogs for arriving in Australia without permission, Joyce had presented himself as a family man devoted to his wife of 24 years and their four daughters.

But rumors had circulated for months in political circles that he was having an affair with a former journalist who had joined his political staff. When a tabloid newspaper published photographs on Feb. 6 of the woman, heavily pregnant, crossing a suburban street, the 50-year-old Joyce acknowledged that they were in a relationship. Australian media outlets say she is 33.

He tried to rally support by arguing the media shouldn’t pry into politicians’ private lives, a position that even some journalists agreed with and which he repeated at his resignation announcement.

“That’s just not who we are in Australia,” he told reporters in his country district. “Don’t go after private individuals. It’s just wrong.”

But Prime Minister Malcolm Turnbull, who is in Washington for talks with President Trump, was aghast at Joyce’s behavior, according to people who know him, and introduced a rule prohibiting sexual relations between ministers and their personal staff, a decision that tapped into the global #MeToo movement inspired by the allegations against movie producer Harvey Weinstein.

Although a poll showed most Australians agreed with what social media dubbed the “bonk ban,” some members of Turnbull’s center-right Liberal Party and other government ministers were uncomfortable with what essentially ended an unwritten convention that politicians’ sex lives are never discussed publicly.

“It licenses the press to investigate politicians’ private lives,” said Tom Switzer, the executive director of the Center for Independent Studies think tank who had sought Liberal Party endorsement for federal office. “It’s an invasion of privacy.”

Despite three weeks of negative publicity and open tension with Turnbull, Joyce seemed determined to keep his position. But news this week that a woman had complained to the Nationals Party, which is in a coalition with the Liberal Party, that he had sexually harassed her led Joyce to resign.

The woman’s name or the nature of the allegation isn’t publicly known. Joyce said he wanted the allegation to be referred to the police and to be given an opportunity to defend himself.