Hard-core predicates: should the adversary be given $1^n$?
First note that it is not necessary for $B$ to guess the correct $n$. To see this, you can think of the PPT adversary $A'$ that on input  $(y, 1^{n-1})$ outputs $A(y, 1^n)$. The sole purpose of the input $1^n$ is to allow $A$ to run in polytime in $n$.

However, if the input (i.e., the output of $f$) to $B$ is of size, e.g., a constant $k$, then $B$ can not run an adversary $A$ running in polytime of $n$. But in this case it should not be problem for the following reason.  

I assume $U_n$ here roughly means a $n$ uniformly random bits? In that case you have from $f$ being injective and pigeon-hole principle that with high probability (at least $\frac{1}{2}$) $|f(U_n)| \geq n$. I.e., $B$ will be allowed to run in polytime in $n$ at least on these inputs. 

EDIT: Note that the above is slight sloppy and simplified argument. You do not strictly need $|f(U_n)| \geq n$, it could be smaller. You just need it to be large enough for $B$ to run in poly time of $n$.