简体   繁体   中英

iOS UISegmentedControl cannot position programmatically

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.

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.

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;

这可以是“自动布局”,您可以尝试以编程方式创建UISegmentedControl。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM