【问题标题】:Is there a way to make a UISegmentedControl button different sizes?有没有办法让 UISegmentedControl 按钮大小不同?
【发布时间】:2012-12-12 18:45:02
【问题描述】:

我有一个包含 2 个项目的 UISegmentedControl。

有没有办法让左边的项比右边的项大一点?

UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:symbol,@"+", nil]];
        [segmentedControl addTarget:self action:@selector(segmentedControlChanged:)forControlEvents:UIControlEventValueChanged];
        [segmentedControl setWidth:45 forSegmentAtIndex:0];
        [segmentedControl setWidth:20 forSegmentAtIndex:1];
        segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
        segmentedControl.frame = CGRectMake(0,0,300,30);
        segmentedControl.momentary = NO;
        [segmentedControl setSelectedSegmentIndex:0];

        self.navigationItem.titleView = segmentedControl;

没用。

【问题讨论】:

    标签: iphone objective-c cocoa-touch uisegmentedcontrol


    【解决方案1】:

    是的,你可以这样做:-

        [*yoursegmentcontrolobjectname* setWidth:45 forSegmentAtIndex:0];
        [*yoursegmentcontrolobjectname* setWidth:15 forSegmentAtIndex:1];   
    
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:symbol,@"+", nil]];
            [segmentedControl addTarget:self action:@selector(segmentedControlChanged:)forControlEvents:UIControlEventValueChanged];
            segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
            segmentedControl.frame = CGRectMake(0,0,300,30);
            segmentedControl.momentary = NO;
            [segmentedControl setSelectedSegmentIndex:0];
    
            self.navigationItem.titleView = segmentedControl;
    

    现在添加这两行就可以了

            [segmentedControl setWidth:45 forSegmentAtIndex:0];
            [segmentedControl setWidth:20 forSegmentAtIndex:1];
    

    【讨论】:

    • 我尝试使用 setWidth:forSegmentIndex 但没有任何运气。我将 UISegmentedControl 作为标题视图放在 UINavigationBar 中
    • 我认为使用编辑后的答案可以解决您的问题。
    • 有效!感谢您的跟进。狡猾的野兽。
    【解决方案2】:

    当然。您可以使用-setWidth:forSegmentAtIndex:(确保在设置分段控件的边界/框架后这样做),或者只是更改界面文件中的宽度(似乎这在导航栏中不起作用,只能在其他地方使用):

    【讨论】:

    • 我尝试使用 setWidth:forSegmentIndex 但没有任何运气。我将 UISegmentedControl 作为标题视图放在 UINavigationBar 中
    • @Sheehan Alam:你试过直接在 Interface Builder 中做吗?
    • 查看我更新的问题。我正在以编程方式执行此操作,因为我将其添加到我的 UINavBar
    • @Sheehan Alam:不幸的是,我看到了相同的结果。我认为你做不到。
    • UINavigationBar 是一条红鲱鱼。请参阅下面的石膏或我的答案。问题是在设置框架之前调用 setWidth。
    【解决方案3】:

    如果您只想根据其内容调整段元素的大小,请使用此 BOOL 属性:

    apportionsSegmentWidthsByContent

    并将其设置为 YES(默认为 NO)

    【讨论】:

      【解决方案4】:

      iOS 6 更新

      在 iOS 5 及更低版本中,我们必须在设置框架和其他显示属性后调用 setWidth: forSegmentAtIndex: 才能使其正常工作。这在 iOS 6 中得到了纠正,顺序不再重要。

      【讨论】:

        【解决方案5】:

        将您的setWidth:forSegmentAtIndex: 调用放在您的`setFrame:' 调用之后。这应该够了吧。大多数时候,在 Cocoa Touch 中排序并不重要,只要一切都在代码结束之前完成即可。在这种情况下,排序确实很重要。

        UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:symbol,@"+", nil]];
                [segmentedControl addTarget:self action:@selector(segmentedControlChanged:)forControlEvents:UIControlEventValueChanged];
                segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
                segmentedControl.frame = CGRectMake(0,0,300,30);
                [segmentedControl setWidth:45 forSegmentAtIndex:0];
                [segmentedControl setWidth:20 forSegmentAtIndex:1];
                segmentedControl.momentary = NO;
                [segmentedControl setSelectedSegmentIndex:0];
        
                self.navigationItem.titleView = segmentedControl;
        

        【讨论】:

          猜你喜欢
          • 2012-03-09
          • 2017-01-20
          • 2021-09-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-02-28
          • 2020-04-23
          相关资源
          最近更新 更多