简体   繁体   中英

How do I programmatically display a UISegmentedControl?

I'm looking at an iPhone application, with several ViewControllers controlled by a UINavigationController.

Somewhere deep down one of the ViewControllers is creating a view, and the view is trying to pop a

UISegmentedControl *segmentedControl 

up on top of itself like so:

[self.window addSubview:segmentedControl];

"self" is a specialized form of UIView, and the control's frame is set

 segmentedControl.frame = someFrame;

For some reason, the control is not showing up on top of the current view. Should this work, and, if it should, what are the most likely causes of trouble?

When you say self is a specialized form of UIView I'm guessing you are referring to a UIView custom subclass.

If you are adding the UISegmentedControl from within this view, you want to do the following:

[self addSubview:segmentedControl];

You set it's frame relative to it's superview, so if you want it on the top left corner you would do:

segmentedControl.frame = CGRectMake(0.0f, 0.0f, someWidth, someHeight);

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