简体   繁体   English

从另一个视图IOS更改滚动条背景

[英]Change Scrollbar background from another view IOS

This is HomeView enter image description here 这是HomeView 在此处输入图像描述

When Someone tapped in 'Banking' button . 当有人点击“银行”按钮时。 Action will load BaseviewController . 动作将加载BaseviewController。

This is the button action. 这是按钮动作。

HomeViewController.m HomeViewController.m

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

This is BaseView enter image description here 这是BaseView 在此处输入图像描述

There is a scrollbar which will load every view . 有一个滚动条将加载每个视图。 The scrollbar background is Green . 滚动条背景为绿色。 And change the background in next here is the problem . 而在接下来改变背景是这里的问题。

BaseViewController.m 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];

 }

When tapped 'Service Request' . 点击“服务请求”时。

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

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

This will load same as like second image I uploaded here . 这将加载与我在此处上传的第二张图片相同的图片。 I just want to change my background color of the scrollbar I want to change scrollbar into black color . 我只想将滚动条的背景颜色更改为黑色。

I have no idea . 我不知道 。 If someone explain me ! 如果有人向我解释! Thanks in advance . 提前致谢 。

APPROACH 1: Instead of initialising scrollBar in each ViewController, why not alloc init it in BaseView Controller and pass instance of it to all View Controller. 方法1:与其在每个ViewController中初始化scrollBar,不如在BaseView Controller中分配它的初始化并将其实例传递给所有View Controller。 Since now all ViewControllers will have same instance it will be easy to change background of the scrollBar. 从现在开始,所有ViewController都将具有相同的实例,因此可以轻松更改scrollBar的背景。

APPROACH 2: You can post notifications whenever you want to change the scrollBar color, add observer for the notification you are posting and with the notification you can pass the color you want to be set for all scrollBar. 方法2:每当您想更改scrollBar颜色时,都可以发布通知,为要发布的通知添加观察者,并使用通知可以传递要为所有scrollBar设置的颜色。

Posting Notification when you want to change color 要更改颜色时发布通知

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

Adding Observer in viewDidLoad 在viewDidLoad中添加观察者

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

Method to handle received Notification 处理收到的通知的方法

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

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

}

REMOVING OBSERVER in viewDidUnload 在viewDidUnload中删除OBSERVER

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM