If Italy joins the CPs in 1916 and Spain chose to follow them, Spain could probably seize it while Britain and its allies get overwhelmed elsewhere in 1917.
Russia joins the Central Powers instead of Austria-Hungary and Spain joins alongside them for some reason. Just sit the war out and watch Britain get smashed, Spain takes Gibraltar from there.