Подтвердить что ты не робот

Как изменить цвет границы UISegmentedControl в iOS7?


Как изменить цвет рамки сегментированного контроллера в iOS7 без изменения цвета текста?


Было бы идеально, если бы я мог поддерживать линию между сегментами как есть (то есть тот же цвет, что и текст), но если изменение цвета границы подразумевает изменение этой строки, это также будет нормально.

Обратите внимание также, что текст (и линии между сегментами) имеет цвет, который установлен с помощью [segmCtrl setTintColor:choosenTintColor]

4b9b3361

Ответ 1

Поэтому я сам решил проблему. Мое решение дает границе сегментированного управления другой цвет, ничего больше.

Чтобы изменить только цвет границы сегментированного элемента управления, я поставил еще один сегментированный элемент управления поверх моего старого. Я отключил взаимодействие с пользователем для этого нового, и я установил изображение для выбранного сегмента на nil.

UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 40)];
// Header view for my main view

UISegmentedControl *subCat = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Segm 1", @"Segm 2", @"Segm 3", @"Segm 4", nil]]; 
// The UISegmentedController which I want to change color for

[subCat setFrame:CGRectMake(5, 5, [UIScreen mainScreen].bounds.size.width - 10, 30)];
[subCat setSelectedSegmentIndex:0];

UISegmentedControl *bcg = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@" ", @" ", @" ", @" ", nil]]; 
// The UISegmentedController I put on top of the other one

UIColor *subColor = [UIColor redColor];
[subCat setTintColor:subColor];
[bcg setFrame:CGRectMake(5, 5, [UIScreen mainScreen].bounds.size.width - 10, 30)];
[bcg setTintColor:[UIColor greenColor]];
[bcg setUserInteractionEnabled:NO];
[bcg setSelectedSegmentIndex:0];
[bcg setImage:nil forSegmentAtIndex:0]; // Removing highlight color


[header addSubview:subCat];
[header addSubview:bcg];

[[self view] addSubview:header];

Ответ 2

Связанный ответ действительно отвечает на ваш вопрос, но вы должны читать между строками. Здесь более ясный пример для изменения всех сегментированных стилей управления в приложении:

// Sets the tint color which typically sets the color of the segment images, text, dividers,
// borders, and selected segment. A translucent version of this color is also used to tint a
// segment when it is pressed and transitioning to being selected, as shown on the first
// segment below.
[[UISegmentedControl appearance] setTintColor:[UIColor blackColor]];

// The attributes dictionary can specify the font, text color, text shadow color, and text
// shadow offset for the title in the text attributes dictionary
[[UISegmentedControl appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateNormal];

Для одного элемента управления в приложении:

// Sets the tint color which typically sets the color of the segment images, text, dividers,
// borders, and selected segment. A translucent version of this color is also used to tint a
// segment when it is pressed and transitioning to being selected, as shown on the first
// segment below.
self.segControl.tintColor = [UIColor blackColor];

// The attributes dictionary can specify the font, text color, text shadow color, and text
// shadow offset for the title in the text attributes dictionary
[self.segControl setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateNormal];

Подробнее здесь: https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/UIKitUICatalog/UISegmentedControl.html

Ответ 3

Я разрешаю вставлять viewWillAppear mySegmentControl.selectedIndex для всех элементов. Таким образом, цвет оттенков появляется для всех сегментов. Конечно, после выбора всех элементов снова выберите свой элемент по умолчанию.