首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何以编程方式将UISegmentedControl添加到容器视图

如何以编程方式将UISegmentedControl添加到容器视图
EN

Stack Overflow用户
提问于 2011-07-14 11:28:19
回答 5查看 93.2K关注 0票数 66

我该如何定义UISegmentedControl的框架?我希望分段的控制出现在底部的container view,即UIView

EN

回答 5

Stack Overflow用户

发布于 2011-07-14 15:03:49

这个很完美,我测试过了.

代码语言:javascript
复制
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 435)];
scroll.contentSize = CGSizeMake(320, 700);
scroll.showsHorizontalScrollIndicator = YES;

NSArray *itemArray = [NSArray arrayWithObjects: @"One", @"Two", @"Three", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(35, 200, 250, 50);
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
[segmentedControl addTarget:self action:@selector(MySegmentControlAction:) forControlEvents: UIControlEventValueChanged];
segmentedControl.selectedSegmentIndex = 1;     
[scroll addSubview:segmentedControl];
[segmentedControl release]; 
[self.view addSubview:scroll];

然后在你的类中添加你的方法。

代码语言:javascript
复制
- (void)MySegmentControlAction:(UISegmentedControl *)segment 
{    
    if(segment.selectedSegmentIndex == 0)
    {
        // code for the first button
    } 
}

对于不推荐使用的UISegmentedControlStyle,您可以查看this URL。

票数 182
EN

Stack Overflow用户

发布于 2017-03-23 20:00:20

请尝试此链接:

ADDING A SEGMENTED CONTROL PROGRAMMATICALLY

更新:

代码语言:javascript
复制
import UIKit

class ViewController: UIViewController {

  /**
    Loads the view and in our case we override default loadView to provide
    custom SegmentedControl.
  */
  override func loadView() {
      super.loadView()
      self.view.backgroundColor = UIColor.purpleColor()

      println("Main view's loadView() called.")

      // Initialize
      let items = ["Purple", "Green", "Blue"]
      let customSC = UISegmentedControl(items: items)
      customSC.selectedSegmentIndex = 0

      // Set up Frame and SegmentedControl
      let frame = UIScreen.mainScreen().bounds
      customSC.frame = CGRectMake(frame.minX + 10, frame.minY + 50,
                                  frame.width - 20, frame.height*0.1)

      // Style the Segmented Control
      customSC.layer.cornerRadius = 5.0  // Don't let background bleed
      customSC.backgroundColor = UIColor.blackColor()
      customSC.tintColor = UIColor.whiteColor()

      // Add target action method
      customSC.addTarget(self, action: "changeColor:", forControlEvents: .ValueChanged)

      // Add this custom Segmented Control to our view
      self.view.addSubview(customSC)
  }

  /**
    Handler for when custom Segmented Control changes and will change the
    background color of the view depending on the selection.
   */
  func changeColor(sender: UISegmentedControl) {
      println("Change color handler is called.")
      print("Changing Color to ")
      switch sender.selectedSegmentIndex {
      case 1:
          self.view.backgroundColor = UIColor.greenColor()
          println("Green")
      case 2:
          self.view.backgroundColor = UIColor.blueColor()
          println("Blue")
      default:
          self.view.backgroundColor = UIColor.purpleColor()
          println("Purple")
      }
  }

  override func viewDidLoad() {
      super.viewDidLoad()
      // Do any additional setup after loading the view, typically from a nib.
  }

  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.
  }
}

来源:http://www.richardhsu.me/

票数 35
EN

Stack Overflow用户

发布于 2013-11-16 13:52:43

你可以这样做。

代码语言:javascript
复制
UISegmentedControl *segmentControl = [[UISegmentedControl alloc]initWithItems:@[@"One",@"Two"]];

[segmentControl setSegmentedControlStyle:UISegmentedControlStyleBar];
segmentControl.frame = CGRectMake(10, 50, 300, 30);
[segmentControl addTarget:self action:@selector(segmentedControlValueDidChange:) forControlEvents:UIControlEventValueChanged];
[segmentControl setSelectedSegmentIndex:0];
[scrollView addSubview:segmentControl];
[segmentControl release];

第2步:

代码语言:javascript
复制
-(void)segmentedControlValueDidChange:(UISegmentedControl *)segment
{
switch (segment.selectedSegmentIndex) {
    case 0:{
        //action for the first button (Current)
        break;}
    case 1:{
        //action for the first button (Current)
        break;}
    }
}
票数 21
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6688160

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档