I'm trying to create my own Beamer theme and I have a problem in handling the headline. I would like to create a headline with a coloured line for the frame title, while for other frames I want a classical headline with sections and possibly subsections. Here is the part of the code I use:

\defbeamertemplate*{headline}
\leavevmode%
\hbox{%
\ifx\insertsection\@empty
\else
{
\ifx\insertsubsection\@empty
\else
{\hspace{0.5em}$\vert$ \hspace{0.5em}{\usebeamerfont{headsubsection}\color{DarkGray}\insertsubsection}}%
\fi
}
\fi
\end{beamercolorbox}%
} %
\vskip0pt%
\else
\hbox{%
\begin{beamercolorbox}[wd=\paperwidth,ht=20pt,dp=0pt]{linemid}
\end{beamercolorbox}%
}
\fi
}


In such a way I obtain a result which is not satisfactory because the coloured line in title frame is OK, but sections and subsection of the following frames (and so the title and the body of the frame) are shifted down. It seems that beamer keeps the space reserved for the first headline, but I don't want that. Is there a way to fix the problem?

That's right, the beamer package only calculates the height of the headline once, at the beginning of the document. The macro responsible for this task is called \beamer@calculateheadfoot, and is defined in beamerbaseframecomponents.sty (ll. 161-182). If the head of your headline changes in the course of the presentation (which doesn't sound like a terribly good idea to my mind, BTW), you'll have to issue this command manually.

This can be done for example by adding it to the internal macro \beamer@@@@frame: Like this, the head of head- and footlines etc. are recalculated each time a frame is being built:

\usepackage{etoolbox}
\makeatletter
\pretocmd{\beamer@@@@frame}{%
\makeatother


Add this code snippet to your document or your theme file. (The manual change of framenumber is necessary to get the correct headline as beamer hasn't stepped the counter at this point.)

### Minimal example:

\documentclass{beamer}

% To make a compilable example, these definitions are necessary - you can replace them by your own
\colorlet{LightBlue}{blue}
\colorlet{DarkGray}{gray}
\setbeamercolor{linemid}{bg=green}

\makeatletter
\leavevmode%
\hbox{%
\ifx\insertsection\@empty
\else
{
\ifx\insertsubsection\@empty
\else
{\hspace{0.5em}$\vert$ \hspace{0.5em}{\usebeamerfont{headsubsection}\color{DarkGray}\insertsubsection}}%
\fi
}
\fi
\end{beamercolorbox}%
} %
\vskip0pt%
\else
\hbox{%
\begin{beamercolorbox}[wd=\paperwidth,ht=20pt,dp=0pt]{linemid}
\end{beamercolorbox}%
}
\fi
}

% (http://tex.stackexchange.com/a/61788/3323)
\usepackage{etoolbox}
\makeatletter
\pretocmd{\beamer@@@@frame}{%