King Felipe VI: Spain and UK 'profoundly intertwined'
Britain and Spain can overcome their differences and maintain strong ties after Brexit, the king of Spain has said in a speech at Westminster.
King Felipe VI said he believed they could begin "the necessary dialogue" to form an arrangement over Gibraltar.
But the government of Gibraltar said the king's focus on a dialogue between London and Madrid was "undemocratic".
The start of a three-day state visit to the UK by the king and queen of Spain ended with a Buckingham Palace banquet.
King Felipe made his comments on Gibraltar in a speech in the Palace of Westminster.
While discussing Britain's decision to leave the EU, he said: "To overcome our differences will be greater in the case of Gibraltar. I am confident through the necessary dialogue and effort, our two governments will be able to work... towards arrangements that are acceptable to all involved."
The government of Gibraltar said it would have to be involved in any discussion between Spain and the UK.
It added that two referenda in 1967 and 2002 showed the people of Gibraltar voted to remain British.
Chief minister Fabian Picardo QC said: "We have no desire to part of Spain or to come under Spanish sovereignty in any shape or form.
"In the times in which we live, territories cannot be traded from one monarch to another like pawns in a chess game."
During the speech, King Felipe said Britain and Spain were "profoundly intertwined" and he respected the UK's decision to leave the EU.
Hundreds of thousands of Britons live in Spain, and a similar number of Spaniards live in the UK, King Felipe told MP and peers.
They "form a sound foundation for our relations," he added.
"These citizens have a legitimate expectation of stable living conditions for their families," he said.
The king highlighted the two countries' important trading arrangements, adding that Britain is "the second largest investor in our country".
At the banquet later hosted by the Queen and Prince Philip at Buckingham Palace, the British monarch acknowledged the two countries had not always seen "eye to eye".
In a speech, she also said: "A relationship like ours founded on such great strengths and common interests will ensure that both our nations prosper now and in the future whatever challenges arise."
The banquet menu began with poached fillet of salmon trout with fennel. It was followed by a medallion of Scottish beef with bone marrow and truffles, with a sauce made from Madeira, and a dark chocolate and raspberry tart for dessert.
The Duke and Duchess of Cambridge, Prince Harry, the Prince of Wales, the Duchess of Cornwall, the Princess Royal, the Duke of York and the Earl and Countess of Wessex also attended.
Earlier the Queen and the Duke of Edinburgh greeted King Felipe and Queen Letizia at Horse Guards Parade, in a traditional welcoming ceremony.
The trip is the first state visit by a Spanish king to the UK since Felipe's father, Juan Carlos, came 31 years ago.
The Queen gifted King Felipe copies of love letters from his great-grandmother to King Alfonso XIII.
Queen Victoria's grand-daughter Princess Victoria Eugenie met King Alfonso on a state visit to Britain in 1905.
The pair married and Princess Victoria Eugenie became Queen Ena of Spain, making King Felipe a descendant of Queen Victoria.
A royal welcome: Sarah Campbell, BBC royal correspondent
The wind died down and the sun broke through the clouds just as the Queen and Duke of Edinburgh stepped on to the dais at Horse Guards.
Every visiting head of state gets the same welcome - their national anthem and the chance to inspect the guard of honour with Prince Philip. With his retirement imminent, this could be the last time he performed that particular public duty.
Then King Felipe stepped into a carriage with the Queen for the traditional procession down the Mall accompanied by the Household Cavalry. The Duke of Edinburgh and Queen Letizia travelled in a separate carriage.
It was a chance for Britain to show off how well it can do "pomp".
On Thursday, Prince Harry will accompany the royal visitors to Westminster Abbey.
King Felipe will lay a wreath at the Grave of the Unknown Warrior and the prince will join them on a short tour of the abbey, including the Tomb of Eleanor "Leonor" of Castile - the 13th-Century Spanish princess who married Edward I.