Mounting an electrical box to screws drilled into the ceiling
You pull the wiring out of the box enough to allow you to attach the screws safely without nicking a wire.  It really helps to hand-drive the screw with care, not just smash it in with a power-driver. 

If you don't like all that care, then you use a type of junction box that has ears on the outside, like this.  



Or a metal box which, while you do not see any ears, they come in the accessory kit and you can attach them one of several ways.