SegmentedControlNoSegment指定为UISe

SegmentedControlNoSegment指定为UISe

本文介绍了将UISegmentedControlNoSegment指定为UISegmentedControl的selectedSegmentIndex不会对iOS 13产生影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 STAControls 的维护者,该子类继承了各种UIControl,其中一个是.在该项目的示例应用程序中,我有以下代码(可以从上述链接下载):

- (void)textFieldDidEndEditing:(UITextField *)textField {
    NSInteger integer = [textField.text integerValue];
    if (integer < 0 || integer >= self.segmentedControl.numberOfSegments) { this doesn't work
        self.segmentedControl.selectedSegmentIndex = UISegmentedControlNoSegment;
    } else { // this works
        self.segmentedControl.selectedSegmentIndex = integer;
    }
}

在iOS 13中,不遵守UISegmentedControlNoSegment分配:

虽然在iOS 12和更低版本的iOS中,它的运行情况还不错:

关于如何解决此问题的任何想法?

解决方案

已解决并在 STASegmentedControl .我做了以下事情:

- (void)setSelectedSegmentIndex:(NSInteger)selectedSegmentIndex {
    if (@available(iOS 13.0, *)) {
        if (selectedSegmentIndex == UISegmentedControlNoSegment && self.selectedSegmentIndex >= 0
            && !self.handlingNoSegment)
        {
            self.handlingNoSegment = YES;
            NSUInteger index = self.selectedSegmentIndex;
            NSString *title = [self titleForSegmentAtIndex:self.selectedSegmentIndex];
            [self removeSegmentAtIndex:self.selectedSegmentIndex animated:NO];
            [self insertSegmentWithTitle:title atIndex:index animated:NO];
        }
        self.handlingNoSegment = NO;
    }
    [super setSelectedSegmentIndex:selectedSegmentIndex];
}

对于整个提交,请此链接.

I am the maintainer of STAControls which subclasses various UIControls, one of which is UISegmentedControl. Within that project's sample app, I have the following code (can be downloaded from the aforementioned link):

- (void)textFieldDidEndEditing:(UITextField *)textField {
    NSInteger integer = [textField.text integerValue];
    if (integer < 0 || integer >= self.segmentedControl.numberOfSegments) { this doesn't work
        self.segmentedControl.selectedSegmentIndex = UISegmentedControlNoSegment;
    } else { // this works
        self.segmentedControl.selectedSegmentIndex = integer;
    }
}

In iOS 13 the UISegmentedControlNoSegment assignment is not respected:

While in iOS 12 and prior versions of iOS this performs just fine:

Any idea(s) on how to work around this?

解决方案

Resolved and added a work around in STASegmentedControl. I did the following:

- (void)setSelectedSegmentIndex:(NSInteger)selectedSegmentIndex {
    if (@available(iOS 13.0, *)) {
        if (selectedSegmentIndex == UISegmentedControlNoSegment && self.selectedSegmentIndex >= 0
            && !self.handlingNoSegment)
        {
            self.handlingNoSegment = YES;
            NSUInteger index = self.selectedSegmentIndex;
            NSString *title = [self titleForSegmentAtIndex:self.selectedSegmentIndex];
            [self removeSegmentAtIndex:self.selectedSegmentIndex animated:NO];
            [self insertSegmentWithTitle:title atIndex:index animated:NO];
        }
        self.handlingNoSegment = NO;
    }
    [super setSelectedSegmentIndex:selectedSegmentIndex];
}

Consult this link for the entire commit.

这篇关于将UISegmentedControlNoSegment指定为UISegmentedControl的selectedSegmentIndex不会对iOS 13产生影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 19:15