The legal constraints have frustrated Trump, who has railed publicly about the rulings, focusing much of his ire on the 9th Circuit Court of Appeals in San Francisco. In November, he called that venue a “thorn in our side” and accused it of being led by an “Obama judge” — an assertion that drew a rebuke from Chief Justice John G. Roberts Jr. Trump’s aides have accused “activist judges” of supporting “open border” policies.

Immigrant rights groups cheered the courts, calling the judicial branch a bulwark against a president seeking to enact an anti-immigration agenda that violates the law and pursuing policies that have worsened the strain on an already overburdened immigration system.

They pointed to the Trump administration’s moves to block asylum seekers and expand criminal prosecutions of parents who enter the country without authorization, which led to chaotic scenes of children housed in cage-like facilities and migrant camps along the U.S.-Mexico border.

“These executive orders that have come through in the last two years on immigration are not about smart policy,” said David Leopold, an immigration attorney in Cleveland. “They are about an extremist ideology and an attack on immigration, not about solving a problem.”

White House allies, however, said they expect Trump to ultimately be vindicated, arguing the Constitution bestows on the executive branch significant unilateral authority on immigration. Some conservatives accused Trump’s opponents of “venue-shopping” for sympathetic judges but predicted the Supreme Court will rule in the administration’s favor in some of the cases, as it did last summer when a majority of justices affirmed a revised version of a ban on travelers from some Muslim-majority countries on national security grounds.

The Justice Department is expected to appeal the New York court’s ruling on the citizenship question, and Trump has publicly expressed optimism that the Supreme Court later this year will uphold his authority to unwind the Deferred Action for Childhood Arrivals program that began under President Barack Obama. The Trump administration declared the program unconstitutional in 2017.

“In most of the cases, the president and his administration are on fairly solid footing legally,” said David Inserra, a policy analyst at the conservative Heritage Foundation. He called the legal challenges an inevitable consequence as opponents seek to slow Trump’s agenda and said there is “a little bit of judge-hunting going on.”

More broadly, Trump’s struggles have highlighted the evolution of how presidents have dealt with immigration since Congress passed the last major overhaul of the system nearly three decades ago. The collapse of efforts at comprehensive legislation during the presidencies of George W. Bush and Obama have led to efforts to make bolder changes through executive power.

For Obama, the first steps came as his administration sought to establish new enforcement priorities that targeted criminals and recent arrivals, while offering reprieves from deportation for undocumented immigrants who had lived in the country for years.

In 2012, during his reelection bid, Obama enacted DACA, which offered renewable two-year work permits to young undocumented immigrants brought to the country as children. A court challenge to the program was defeated, and Obama felt emboldened in November 2014 to announce a major expansion aimed at shielding up to 4 million parents of U.S. citizens and legal permanent residents from deportation.

Obama said he acted reluctantly after House Republicans refused that year to vote on a Senate-approved bipartisan comprehensive immigration bill. But Texas and two dozen other Republican-led states sued the Obama administration, arguing that it violated the proper rulemaking procedures and that the new program would impose onerous costs on states to provide drivers’ licenses and other benefits to the immigrants.

A federal judge in Brownsville, Tex., enjoined the new program, and after an extended legal battle, the Supreme Court split 4 to 4 in 2016, leaving the injunction in place and effectively killing it.

Unlike Obama, Trump did not wait on Congress before pursuing sweeping actions. In his first week, he announced an executive order to roll back Obama’s immigration enforcement priorities and signed the travel ban on foreign nationals from seven majority-Muslim nation.

That ban was enjoined two days later by a federal judge in New York, and several other courts followed suit, sparking a lengthy legal battle that forced the administration to amend its order twice before winning affirmation in the Supreme Court.

The legal hurdles have continued since then. In some cases, Trump has been defeated on constitutional grounds, including rulings that said the administration violated spending authority bestowed on Congress when it sought to withhold grants to sanctuary cities. In other cases, the administration has been halted on procedural grounds, such as Trump’s bid to end DACA, a plan that courts have called “arbitrary” and “based on a flawed legal premise.”

Trump’s policies are “creating more havoc,” said Marielena Hincapié, executive director of the National Immigration Law Center. “Rather than solve problems, Trump is manufacturing ‘crises.’ ”

Since shutting the government down in his fight to win border wall funding, Trump has repeatedly threatened to declare a national emergency and use Pentagon funds to build a border wall, although he has backed off in recent days amid concerns from some Republican allies.

“There is a dramatic difference between how President Trump has rolled out unlawful and unconstitutional policies so frequently that it becomes the norm, as compared to his predecessors, whose respect for the law was far less in question,” said Gregory Chen, government relations director at the American Immigration Lawyers Association.

It’s not just immigration. The Trump administration has suffered legal setbacks on other high-profile actions, including a ban on transgender military troops and rules that allow employers and insurers to decline to provide birth control on religious grounds.

But for both Obama and Trump, there is perhaps a common lesson forged by their struggles on immigration — executive power has its limits. The question is whether congressional lawmakers will ever move to rein in presidents by filling the void that their own inaction has created.