[英]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.