I disagree with David and would tilt north since spending all of your time in Madrid and points south in June (I'm guessing you won't be starting the trip really early in the month) puts you at risk of nothing but hot days. Before making a decision, go to wunderground.com, enter the name of one of the southern cities (Seville is about the hottest), choose History > Monthly, select June 2017, and look at the actual daily temperatures. Repeat for June 2016, June 2015, etc., to get a good idea of the range of weather you might experience. It seems that the average-high-temperature charts really understate the misery level of summer travel to hot destinations. I think part of the problem is that those averages often go back many decades, pre-climate change. I've seen some that do not include years after 2010--which can be extremely misleading.
Barcelona has the Camp Nou soccer experience (books up; reserve your spots way ahead) and a lot of beaches either local or a short trip away, You can take a day trip to the Dali Theatre and Museum in Figueres from Barcelona, but I'd be inclined to spend a few nights in the much less hectic Girona, which has a large historic district with a walkable wall and a very nice small art museum that might be manageable with the kids. I don't remember any surrealists, but staying in Girona would put you a fast train ride from Figueres. It makes for a quicker trip to the little historic town of Besalu as well. Besalu has a cool fortified bridge. There's bus service from Girona. Barcelona to Besalu is also doable but takes longer.
You should not expect cool weather in Barcelona, but it will be milder than Madrid and Andalucia. You also need to understand the magnitude of the tourist traffic in the city. Many of the most popular attractions have long ticket lines, so you need to buy tickets before you walk up to the sight. It is a pain if you want to go to a lot of those busy places, because it's tough to schedule back-to-back entries without knowing how much time you will spend at each place. However, the only local art museum that usually requires advance-purchased tickets is the Picasso (and it is often miserably mobbed--skip it if you are not big fans). The Miro and the MNAC were fine for me in August 2016. The Sant Pau modernista site is another great spot to which you can (so far) just walk up and buy your ticket. And the Barri Gotic can be explored on your own schedule, though I really liked the walking tour conducted by the tourist office. A day trip to Montserrat offers walking/hiking possibilities. There is tons to do in and around Barcelona, and Enric has made a lot of suggestions in earlier posts about activitkes of possible interest to families traveling with children.
I really think the scheduling complications caused by the over-visited top attractions in Barcelona would have less impact on a family like yours, traveling with young children. I can't imagine that you'd want to march them through a lot of modernista-architecture sights; perhaps just Parc Guell?
The Basque Country and the area near the coast all the way west to Galicia is the coolest part of Spain in the summer and an excellent area for tourists who don't mind some cloudy days and possible rain. The Basque Country is known for its food; San Sebastian is a big foodie destination.
Unfortunately, the historic district is also the tapas-bar distict, so it's overrun. Bilbao has a wonderful and larger historic district with much less tourist traffic. Most tourists seem to stop by just long enough for a quick visit tk the Guggenheim, then they are gone; Bilbao is worth more time than that.