2

I cannot change the location of the UISegmentedControl. If I do this:

segControl2.frame = CGRectMake( segControl1.origin.x, segControl2.origin.y, segControl2.size.width, segControl2.size.height);

nothing really changes, and everybody says changing the frame of the control should make it, but it really does not.

Cœur
  • 32,421
  • 21
  • 173
  • 232
user1405583
  • 121
  • 2
  • 10
  • check the answer with the high vote in this question. the answer contains a nice way of adding the uisegment to the view through code. http://stackoverflow.com/questions/6688160/how-to-programmatically-add-a-uisegmentedcontrol-to-a-container-view – Adrian P Mar 20 '13 at 23:17
  • Have you added the subview?? – NULL Mar 20 '13 at 23:22
  • There is probably something else affecting the placement of your segmented control. Please share some more code. – Aaron Feb 19 '14 at 18:04

3 Answers3

1
CGRect frame = [[UIScreen mainScreen] bounds];
UISegmentedControl *segControl = [[UISegmentedControl] initWithItems:[NSArray alloc] initWithObjects:@"One", @"Two", @"Three", nil];
segControl.frame = CGRectMake(frame.origin.x + 50, frame.origin.y + 50, segControl.size.width, segControl.size.height);
[view addSubview:segControl];

Should work just fine.

Bret Deasy
  • 798
  • 5
  • 14
  • What if I have the control that has already been created in the nib, I am not creating it in the code. Why changing the frame won't do the trick then? – user1405583 Mar 21 '13 at 19:10
0

For changing the position of any element after you had added as subview, you can use the following:

CGRect frame = segControl2.frame;
frame.origin.x = segControl1.frame.origin.x;
// for changes to other properties, do likewise. if no changes, don't do anything
frame.origin.y = ...

segControl2.frame = frame;
Rick
  • 1,798
  • 1
  • 14
  • 17
  • This does not work for me. When debugging I am going line by line, so I can clearly see the frame.origina.x really changes after I set x to a smaller value, moving the control to the right, but nothing happens, control does not move. I have control created in nib, and I am doing the code in viewDidLoad method. – user1405583 Mar 21 '13 at 20:16
  • That's strange. I have used this method to position almost all my elements that are created in storyboard or programmatically. Maybe your problem doesn't lie in the code but the parameters that are set in IB or where are you placing the UISegmentedControl. – Rick Mar 22 '13 at 07:14
  • I can change the UISegmentedControlStyle using the very same property of the control, so I guess it is hooked properly. – user1405583 Mar 22 '13 at 08:12
  • So where exactly did you place the UISegmentedControl in the nib? In the navigation bar? In a UIView? – Rick Mar 22 '13 at 15:45
  • It is not the UISementedControl only, I checked it now. I can not move the text boxes either. It is a simple nib, no storyboard, and a simple class with its viewDidLoad method. I am really confused, this should be working. – user1405583 Mar 22 '13 at 19:44
  • Just a hunch, try turning off "autolayout" in your nibs. – Rick Mar 23 '13 at 06:50
0

This can be the "Auto-Layout", you can try to create your UISegmentedControl programmatically instead.

Thomas Decaux
  • 18,451
  • 2
  • 83
  • 95