2010-08-04 71 views
28

我是新手iphone編程,我想開發一個使用頁面控制的應用程序。我的視圖的背景顏色是白色,頁面控制器默認的是白色,這使得頁面控制在我的視圖中不可見,所以我改變了頁面控件的背景顏色以使其可見。現在,視圖出現了修補和不良。有沒有辦法改變頁面控制的點顏色?有沒有辦法改變頁面指示點顏色

在此先感謝

+0

重複的問題。順便說一句,看到這個http://stackoverflow.com/questions/2942636/how-i-change-the-color-of-pagination-dots-of-uipagecontrol,給出的解決方案是世界上最好的之一。我個人使用過很多次。 – Tirth 2013-01-21 06:49:21

+0

你可以參考這篇文章http://stackoverflow.com/questions/26648988/code-to-give-different-color-for-each-pagination-dots-in-ios/26649447#26649447 – 2014-11-06 06:16:09

回答

51

我們定製了UIPageControl使用的頁面指示符的自定義圖像,我在下面列出的類的膽量......

GrayPageControl.h

@interface GrayPageControl : UIPageControl 
{ 
    UIImage* activeImage; 
    UIImage* inactiveImage; 
} 

GrayPageControl.m

-(id) initWithCoder:(NSCoder *)aDecoder 
{ 
    self = [super initWithCoder:aDecoder]; 

    activeImage = [[UIImage imageNamed:@"active_page_image.png"] retain]; 
    inactiveImage = [[UIImage imageNamed:@"inactive_page_image.png"] retain]; 

    return self; 
} 

-(void) updateDots 
{ 
    for (int i = 0; i < [self.subviews count]; i++) 
    { 
     UIImageView* dot = [self.subviews objectAtIndex:i]; 
     if (i == self.currentPage) dot.image = activeImage; 
     else dot.image = inactiveImage; 
    } 
} 

-(void) setCurrentPage:(NSInteger)page 
{ 
    [super setCurrentPage:page]; 
    [self updateDots]; 
} 
在視圖控制器

然後我們只是用它像一個正常的UIPageControl

IBOutlet GrayPageControl* PageIndicator; 

編輯:

在具有GrayPageControl我有一個鏈接到GrayPageControl.ValueChanged事件的IBAction爲視圖控制器。

-(IBAction) pageChanged:(id)sender 
{ 
    int page = PageIndicator.currentPage; 

    // update the scroll view to the appropriate page 
    CGRect frame = ImagesScroller.frame; 
    frame.origin.x = frame.size.width * page; 
    frame.origin.y = 0; 
    [ImagesScroller scrollRectToVisible:frame animated:YES]; 
} 
+2

我還增加了覆蓋layoutSubviews剛剛稱爲updateDots - 它刪除了一個輕微的閃爍,當控制第一次添加:) – deanWombourne 2010-11-22 13:23:41

+0

我只是尋找你的解決方案,並不能得到它的工作。我嘗試在Interface Builder中將* PageIndicator連接到UIPageControl元素,但沒有運氣。我怎樣才能做到這一點? 謝謝! – 2011-06-30 07:26:36

+0

現在有效。我只是在Interface Builder中將PageControl類設置爲UIPageControl。 但現在我想讓「圓點」(圖像)變大。我在updateDots()方法中完成了這個操作: dot.frame = CGRectMake(0,0,20,20); 當我啓動應用程序,它是好的。但在滾動它只顯示我一個。有解決方案嗎? – 2011-06-30 07:37:31

15

JWD答案是要走的路。然而,如果你只想改變顏色,爲什麼不這樣做:

這種技術將只適用於iOS6.0和更高!

enter image description here

選擇UIPageControl去屬性檢查器。田田。

或者你也可以玩這些2個屬性:

pageIndicatorTintColor property 
    currentPageIndicatorTintColor property 

這是如此簡單。我再次閱讀這個問題,以確保我沒有錯。你真的只想改變顏色嗎?好的。

你仍然堅持這個規則點。真棒點圖片使用JWD技術。

+0

我沒有錯,我是嗎?這確實是這個簡單的權利? – 2012-10-22 09:32:41

+0

我不想誇耀。雖然JWD的回答非常好,但我是唯一真正回答這個問題的人:D – 2012-10-30 10:25:45

+0

如果我想要做同樣的事情iOS 5然後我會怎麼做? – Apple 2013-04-24 09:10:45

7

只需一行代碼即可滿足您的需求。該示例設置爲黑色。

pageControl.pageIndicatorTintColor = [UIColor blackColor]; 
17

該應用程序在iOS7中崩潰。我已經爲iOS 7的解決方案:

原因崩潰:

在iOS的7 [self.subViews objectAtIndex: i]回報 UIView代替 UIImageViewsetImage

不是UIView的屬性和應用程序崩潰。我使用下面的代碼解決了我的問題。

檢查子視圖是否爲UIView(適用於iOS7)或UIImageView(適用於iOS6或更低版本)。如果它是UIView我要添加UIImageView作爲該視圖的子視圖,並說它的工作,而不是崩潰..!

-(void) updateDots 
{ 
    for (int i = 0; i < [self.subviews count]; i++) 
    { 
     UIImageView * dot = [self imageViewForSubview: [self.subviews objectAtIndex: i]]; 
     if (i == self.currentPage) dot.image = activeImage; 
     else dot.image = inactiveImage; 
    } 
} 
- (UIImageView *) imageViewForSubview: (UIView *) view 
{ 
    UIImageView * dot = nil; 
    if ([view isKindOfClass: [UIView class]]) 
    { 
     for (UIView* subview in view.subviews) 
     { 
      if ([subview isKindOfClass:[UIImageView class]]) 
      { 
       dot = (UIImageView *)subview; 
       break; 
      } 
     } 
     if (dot == nil) 
     { 
      dot = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, view.frame.size.width, view.frame.size.height)]; 
      [view addSubview:dot]; 
     } 
    } 
    else 
    { 
     dot = (UIImageView *) view; 
    } 

    return dot; 
} 

希望這個解決你的問題也適用於iOS7。如果Anypone找到最佳解決方案,請評論。 :)

快樂編碼

+0

如果您不想在背景上使用白色圓角顏色,只需添加view.backgroundColor = [UIColor clearColor];在圖像開始處eViewForSubview方法。 – 2014-06-30 14:48:12

3

,如果你只是想改變點的顏色或自定義它,你可以把它作爲建議JWD,但在點點另一種方式:

#pragma mark - LifeCycle 

- (void)setCurrentPage:(NSInteger)page 
{ 
    [super setCurrentPage:page]; 
    [self updateDots]; 
} 

#pragma mark - Private 

- (void)updateDots 
{ 
    for (int i = 0; i < [self.subviews count]; i++) { 
     UIView* dot = [self.subviews objectAtIndex:i]; 
     if (i == self.currentPage) { 
      dot.backgroundColor = UIColorFromHEX(0x72E7DB); 
      dot.layer.cornerRadius = dot.frame.size.height/2; 
     } else { 
      dot.backgroundColor = UIColorFromHEX(0xFFFFFF); 
      dot.layer.cornerRadius = dot.frame.size.height/2 - 1; 
      dot.layer.borderColor = UIColorFromHEX(0x72E7DB).CGColor; 
      dot.layer.borderWidth = 1; 
     } 
    } 
} 

至於導致你將得到的東西像

enter image description here

而且宏:

#define UIColorFromHEX(hexValue) [UIColor colorWithRed:((float)((hexValue & 0xFF0000) >> 16))/255.0 green:((float)((hexValue & 0xFF00) >> 8))/255.0 blue:((float)(hexValue & 0xFF))/255.0 alpha:1.0] 
+0

要正常工作,您必須禁用用戶交互。否則有一個小故障! – fingerup 2016-05-31 13:13:32

4

您可以更改頁面指示符色調的顏色和用下面的代碼當前頁面指示符色調顏色:

pageControl.pageIndicatorTintColor = [UIColor orangeColor]; 
pageControl.currentPageIndicatorTintColor = [UIColor blackColor]; 
2

做到這一點的最好和最賽普爾方法是添加以下代碼行在AppDelegate.m

UIPageControl *pageControl = [UIPageControl appearance]; 
pageControl.pageIndicatorTintColor = [UIColor lightGrayColor]; 
pageControl.currentPageIndicatorTintColor = [UIColor blackColor]; 
pageControl.backgroundColor = [UIColor whiteColor]; 
相關問題