Comment pouvons-nous changer la couleur de l'indicateur de défilement d'UIScrollview en bleu, vert, etc.?
Je sais que nous pouvons le changer en blanc, noir. Mais autre que ces couleurs.
Merci beaucoup
Malheureusement, vous ne pouvez pas, bien sûr, vous pouvez toujours lancer le vôtre. Ce sont vos options:
UIScrollViewIndicatorStyleDefault:
Le style par défaut de l'indicateur de défilement, qui est noir avec une bordure blanche. Ce style convient à tous les arrière-plans de contenu.
UIScrollViewIndicatorStyleBlack:
Un style d'indicateur qui est noir et plus petit que le style par défaut. Ce style convient bien sur un fond blanc.
UIScrollViewIndicatorStyleWhite:
Un style d'indicateur est blanc et plus petit que le style par défaut. Ce style convient bien sur un fond noir.
Voici plus sûr Swift 3 méthode:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let verticalIndicator = scrollView.subviews.last as? UIImageView
verticalIndicator?.backgroundColor = UIColor.green
}
Les deux indicateurs UIScrollView sont une sous-vue de UIScrollView. Nous pouvons donc Accéder à la sous-vue de UIScrollView et modifier la propriété de la sous-vue.
1 .Add UIScrollViewDelegate
@interface ViewController : UIViewController<UIScrollViewDelegate>
@end
2. Ajouter scrollViewDidScroll dans la section implémentation
-(void)scrollViewDidScroll:(UIScrollView *)scrollView1
{
//get refrence of vertical indicator
UIImageView *verticalIndicator = ((UIImageView *)[scrollView.subviews objectAtIndex:(scrollView.subviews.count-1)]);
//set color to vertical indicator
[verticalIndicator setBackgroundColor:[UIColor redColor]];
//get refrence of horizontal indicator
UIImageView *horizontalIndicator = ((UIImageView *)[scrollView.subviews objectAtIndex:(scrollView.subviews.count-2)]);
//set color to horizontal indicator
[horizontalIndicator setBackgroundColor:[UIColor blueColor]];
}
Remarque: - Parce que ces indicateurs sont mis à jour chaque fois que vous faites défiler (Signifie réinitialiser les paramètres par défaut). SO, nous avons mis ce code dans la méthode déléguée scrollViewDidScroll .
Démo disponible sur GitHub - https://github.com/ioshacks/UIScrollViewIndicatorColor
Swift 2.0:
Ajouter un délégué UIScrollView.
func scrollViewDidScroll(scrollView: UIScrollView){
let verticalIndicator: UIImageView = (scrollView.subviews[(scrollView.subviews.count - 1)] as! UIImageView)
verticalIndicator.backgroundColor = UIColor.greenColor()
let horizontalIndicator: UIImageView = (scrollView.subviews[(scrollView.subviews.count - 2)] as! UIImageView)
horizontalIndicator.backgroundColor = UIColor.blueColor()
}
Vous pouvez changer une image d'indicateur, mais vous devriez le faire de manière répétée
func scrollViewDidScroll(_ scrollView: UIScrollView) {
self.chageScrollIndicator()
}
func chageScrollIndicator (){
if let indicator = self.collection.subviews.last as? UIImageView {
let Edge = UIEdgeInsets(top: 1.25,
left: 0,
bottom: 1.25,
right: 0)
indicator.image = UIImage(named: "ScrollIndicator")?.withRenderingMode(.alwaysTemplate).resizableImage(withCapInsets: Edge)
indicator.tintColor = UIConfiguration.textColor
}
}
Si vous souhaitez ajouter une image, voici le code pour Swift 3
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let verticalIndicator = scrollView.subviews.last as? UIImageView
verticalIndicator?.image = UIImage(named: "imageName")
}
Cela fonctionne également pour UITableView et UICollectionView.
Voici comment la couleur de la barre de défilement est modifiée:
//scroll view
UIScrollView *scView = [[UIScrollView alloc] init];
scView.frame = self.view.bounds; //scroll view occupies full parent views
scView.contentSize = CGSizeMake(400, 800);
scView.backgroundColor = [UIColor lightGrayColor];
scView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
scView.showsHorizontalScrollIndicator = NO;
scView.showsVerticalScrollIndicator = YES;
scView.scrollEnabled = YES;
[self.view addSubview: scView];
J'ai écrit un article à ce sujet il n'y a pas si longtemps. Malheureusement, la couleur de ces barres est définie par des images prédéfinies. Par conséquent, si vous envisagez de modifier la couleur des barres, il vous faudra un travail supplémentaire. Jetez un coup d'œil au lien suivant, vous trouverez certainement une réponse ici car j'ai essayé de résoudre le même problème.
http://leonov.co/2011/04/uiscrollviews-scrollbars-customization/
Essayez ceci il vous aiderait certainement
for ( UIView *view in scrollBar.subviews ) {
if (view.tag == 0 && [view isKindOfClass:UIImageView.class])
{
UIImageView *imageView = (UIImageView *)view;
imageView.backgroundColor = [UIColor yellowColor];
}
}
Explication : UIScrollBar est un ensemble de sous-vues. Ici, l'indicateur scrollBar (vertical/horizontal) est l'une des sous-vues et il s'agit d'un UIImageView.So, si nous définissons une couleur personnalisée pour l'indicateur UIImageView it scrollBar.
veuillez utiliser le code ci-dessous sur iOS Renderer
private bool _layouted;
public override void LayoutSubviews()
{
base.LayoutSubviews();
if (!_layouted)
{
this.Layer.BorderColor = UIColor.Red.CGColor;
var Verticalbar = (UIImageView)this.Subviews[this.Subviews.Length - 1];
Verticalbar.BackgroundColor = Color.FromHex("#0099ff").ToUIColor();
var Horizontlebar = (UIImageView)this.Subviews[this.Subviews.Length - 2];
Horizontlebar.BackgroundColor = Color.FromHex("#0099ff").ToUIColor();
_layouted = true;
}
}
Voici ce que j'ai fait dans Swift 4, semblable aux réponses précédentes. Dans mon cas, je recolore l'image pour qu'elle soit invisible, définissez le rayon de coin correct et n'exécutez ce processus qu'une seule fois.
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let color = UIColor.red
guard
let verticalIndicator = scrollView.subviews.last as? UIImageView,
verticalIndicator.backgroundColor != color,
verticalIndicator.image?.renderingMode != .alwaysTemplate
else { return }
verticalIndicator.layer.masksToBounds = true
verticalIndicator.layer.cornerRadius = verticalIndicator.frame.width / 2
verticalIndicator.backgroundColor = color
verticalIndicator.image = verticalIndicator.image?.withRenderingMode(.alwaysTemplate)
verticalIndicator.tintColor = .clear
}
J'ai rencontré le même problème récemment, alors j'ai décidé d'écrire une catégorie pour cela.
https://github.com/stefanceriu/UIScrollView-ScrollerAdditions
[someScrollView setVerticalScrollerTintColor:someColor];
[someScrollView setHorizontalScrollerTintColor:someColor];`
Il le mélange avec l'image d'origine pour que seule la couleur change. D'autre part, il peut également être modifié pour fournir une image personnalisée à utiliser par les défilement.