UISegmentedControll在选择段时更改高度

时间:2014-04-10 11:50:08

标签: ios objective-c

我已使用常量在Storyboard中设置我的UISegmentedControl的高度。当我运行应用程序时 - 它看起来很好,我希望它看起来如何。但是,只要我选择一个片段(任何人),UiSegmentedControl就会将其高度更改为默认值。如果我再次选择当前索引几次,它将返回到我设置的高度。

有人可以告诉我这里发生了什么吗?

谢谢!

修改一些代码:

此方法从viewDidLoad

调用
    -(void)setupSegmented
{
    self.segmentedControl.tintColor = [UIColor clearColor];

    if ([UIImage instancesRespondToSelector:@selector(imageWithRenderingMode:)]) {

        /* For non Selected Segments */
            self.allInactive = [[UIImage imageNamed:@"Segment All"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            self.auctionInactive = [[UIImage imageNamed:@"Segment Auction"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            self.buyNowInactive = [[UIImage imageNamed:@"Segment Buy Now"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            self.classifiedInactive = [[UIImage imageNamed:@"Segment Classified"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

        /*For selected Segments */
            self.allActive = [[UIImage imageNamed:@"Segment All Active"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            self.auctionActive = [[UIImage imageNamed:@"Segment Auction Active"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            self.buyNowActive = [[UIImage imageNamed:@"Segment Buy Now Active"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            self.classifiedActive = [[UIImage imageNamed:@"Segment Classified Active"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }

    [self.segmentedControl setImage:self.allInactive forSegmentAtIndex:0];
    [self.segmentedControl setImage:self.auctionInactive forSegmentAtIndex:1];
    [self.segmentedControl setImage:self.buyNowInactive forSegmentAtIndex:2];
    [self.segmentedControl setImage:self.classifiedInactive forSegmentAtIndex:3];

}

当我点击一个片段时调用这个方法:

    - (IBAction)segmentedControlSelected:(UISegmentedControl *)sender {

    switch (sender.selectedSegmentIndex){

    case 0:
        {
            self.tradeType = @"UNSPECIFIED";
            [self.segmentedControl setImage:self.allActive forSegmentAtIndex:sender.selectedSegmentIndex];
            [self.segmentedControl setImage:self.auctionInactive forSegmentAtIndex:1];
            [self.segmentedControl setImage:self.buyNowInactive forSegmentAtIndex:2];
            [self.segmentedControl setImage:self.classifiedInactive forSegmentAtIndex:3];

            break;
        }
    case 1:
        {
            self.tradeType = @"ENGLISH_AUCTION";
            [self.segmentedControl setImage:self.auctionActive forSegmentAtIndex:sender.selectedSegmentIndex];
            [self.segmentedControl setImage:self.allInactive forSegmentAtIndex:0];
            [self.segmentedControl setImage:self.buyNowInactive forSegmentAtIndex:2];
            [self.segmentedControl setImage:self.classifiedInactive forSegmentAtIndex:3];

            break;
        }
    case 2:
        {
            self.tradeType = @"FIXED_PRICE";
            [self.segmentedControl setImage:self.buyNowActive forSegmentAtIndex:sender.selectedSegmentIndex];
            [self.segmentedControl setImage:self.allInactive forSegmentAtIndex:0];
            [self.segmentedControl setImage:self.auctionInactive forSegmentAtIndex:1];
            [self.segmentedControl setImage:self.classifiedInactive forSegmentAtIndex:3];

            break;
        }
    case 3:
        {
            self.tradeType = @"CLASSIFIED_CONTACT";
            [self.segmentedControl setImage:self.classifiedActive forSegmentAtIndex:sender.selectedSegmentIndex];
            [self.segmentedControl setImage:self.allInactive forSegmentAtIndex:0];
            [self.segmentedControl setImage:self.auctionInactive forSegmentAtIndex:1];
            [self.segmentedControl setImage:self.buyNowInactive forSegmentAtIndex:2];

            break;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

我在这个SO答案中找到了解决方案:Change Height of UISegmentedControl

每次点击-(void)viewDidLayoutSubViews时,我必须强制调整UISegmentedControl中的新高度。