So I have two UIImages : centre and attraction. and when I add attraction as a subview of centre : [centre addSubview:attraction];
at a precise position in my .xib (interface builder) I have to move it to another position to have the position that I would like.To sum up when I add a subview, this subview has a different position I don't know why. How can I solve this please ? sorry for my english I'm french :/

- 19
- 1
- 5
-
attraction will be added wrf to frame of centre...change frame of attraction accordingly – iAmitWagh Oct 12 '11 at 11:54
-
Setting the `frame` as already suggested - see [http://stackoverflow.com/questions/3518804/how-to-addsubview-with-a-position][post]. – petert Oct 12 '11 at 12:16
-
yes but my problem is that centre is moving with an accelerometer and I don't want a fix position for 'attraction' – valentin gougeau Oct 12 '11 at 14:39
-
R u want ur Attarction also want move. – EXC_BAD_ACCESS Oct 13 '11 at 04:16
-
yes that is exactly what I want – valentin gougeau Oct 13 '11 at 15:19
2 Answers
It sounds like you are adding the subview programmatically (since you are showing a call to addSubview in code) but wish the positioning to be done in Interface Builder. You should choose one or the other. Either assign the subview (drag the attraction UIImage so that it shows indented below the centre UIImage in your object hierarchy in Interface Builder) and position it in Interface Builder, or do it all programmatically. The position is relative to the parent, so when you programmatically change the parent it's not surprising that you see a different placement.

- 15,250
- 7
- 58
- 89
-
yes but my problem is that centre is moving with an accelerometer and I don't want a fix position for 'attraction' – valentin gougeau Oct 12 '11 at 14:48
-
If you are going to move 'attraction', the simplest is probably to do all its positioning programmatically and not care at all about how it is positioned in your xib at all. Just programmatically set the frame property of your 'attraction' object. Remember that (after you've called [center addSubview:attraction]) its position will be relative to it. If that doesn't suit (maybe you'll get clipping issues) then just remove [center addSubview:attraction] and make sure your attraction object is 'above' center by simply reordering them in you xib if necessary. – Clafou Oct 14 '11 at 08:40
your problem is with the frame setting.SubView's Frame will be based on its super view.(ie) your centre's Frame is based from its superView(May be self.view).But attraction takes reference as centre's Frame.

- 2,699
- 2
- 21
- 25
-
yes but my problem is that centre is moving with an accelerometer and I don't want a fix position for 'attraction' – valentin gougeau Oct 12 '11 at 14:48