2010-02-23 4 views
8

Qualcuno potrebbe mostrarmi come ottenere la posizione xey di un UIButton nel cacao?Come ottenere la posizione X e Y di un pulsante dell'interfaccia utente iPhone

Il pulsante si sposta sullo schermo quando si fa clic e si desidera utilizzare la posizione corrente per posizionare un altro UIBUtton relativo ad esso.

Ho provato con

CGRect rect=[myButton imageRectForContentRect]; 

Il compilatore mi dice che myButton potrebbe non rispondere a imageRectForContentRect.

risposta

17

Uso

myButton.frame 

particolare per la posizione, utilizzare myButton.frame.origin.x e myButton.frame.origin.y. Quello sarebbe l'angolo in alto a sinistra.

+0

Dolce! Grazie mille – dubbeat

+0

non c'è .frame per il mio pulsante! – iosMentalist

5
CGPoint position = myButton.frame.origin; 

Questo funziona per qualsiasi UIView.

Il compilatore ha ragione (come è incline ad essere): non esiste un metodo chiamato imageRectForContentRect. C'è, tuttavia, imageRectForContentRect: che accetta un CGRect come argomento. Non devi usarlo per ottenere la posizione.