簡體   English   中英

從另一個視圖IOS更改滾動條背景

[英]Change Scrollbar background from another view IOS

這是HomeView 在此處輸入圖像描述

當有人點擊“銀行”按鈕時。 動作將加載BaseviewController。

這是按鈕動作。

HomeViewController.m

BaseViewController *clickbutton  = [[BaseViewController alloc] initWithNibName:@"BaseViewController" bundle:nil] ;
[self presentViewController:clickbutton animated:YES completion:nil];

這是BaseView 在此處輸入圖像描述

有一個滾動條將加載每個視圖。 滾動條背景為綠色。 而在接下來改變背景是這里的問題。

BaseViewController.m

-(void)ViewDidLoad{

   PriorityViewController  *obj ;

   UINavigationController *nav;

   obj  = [[ PriorityViewController alloc]  initWithNibName:@"PriorityViewController" bundle:nil] ;

   nav = [[UINavigationController alloc] initWithRootViewController:obj];

   nav.view.frame = rect;

   [self.view addSubview:self.nav.view];

 }

點擊“服務請求”時。

  OtherBankTransferViewController *obj;
  obj  = [[OtherBankTransferViewController   alloc]initWithNibName:@"OtherBankTransferViewController" bundle:nil];

[self.navigationController  pushViewController:obj animated:YES  ];

這將加載與我在此處上傳的第二張圖片相同的圖片。 我只想將滾動條的背景顏色更改為黑色。

我不知道 。 如果有人向我解釋! 提前致謝 。

方法1:與其在每個ViewController中初始化scrollBar,不如在BaseView Controller中分配它的初始化並將其實例傳遞給所有View Controller。 從現在開始,所有ViewController都將具有相同的實例,因此可以輕松更改scrollBar的背景。

方法2:每當您想更改scrollBar顏色時,都可以發布通知,為要發布的通知添加觀察者,並使用通知可以傳遞要為所有scrollBar設置的顏色。

要更改顏色時發布通知

NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[UIColor blackColor] forKey:@"someKey"];
[[NSNotificationCenter defaultCenter] postNotificationName: @"TestNotification" object:nil userInfo:userInfo];

在viewDidLoad中添加觀察者

[[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(receiveTestNotification:) 
    name:@"TestNotification"
    object:nil];

處理收到的通知的方法

- (void) receiveTestNotification:(NSNotification *) notification{

    NSDictionary *userInfo = notification.userInfo;
    UIColor *backGroundColor = [userInfo objectForKey:@"someKey"];
    // assign this color to your scrollBar in each ViewController

}

在viewDidUnload中刪除OBSERVER

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"TestNotification" object:nil];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM