最近在做开发时要做一个类似于UIAlertView的控件,做法是创建一个基于UIView的类,在里面进行自定义控件的设置,为了尽量模仿UIAlertView,在这个类里面创建了一个新的UIWindow并将self显示到这个window上
最近在做开发时要做一个类似于UIAlertView的控件,做法是创建一个基于UIView的类,在里面进行自定义控件的设置,为了尽量模仿UIAlertView,在这个类里面创建了一个新的UIWindow并将self显示到这个window上。
由于app中statusbar中的内容为白色的,新创建的window就会改变statusbar的状态,不能得到我们想要的结果,为了避开一系列其他错误,设置statusbar的颜色时采用
- (UIStatusBarStyle)preferredStatusBarStyle
- (void)setNeedsStatusBarAppearanceUpdate
这两个方法,你没看错,这两个方法是UIViewController的方法,那么跟UIView有什么关系呢,不错,这里我们是想给UIWindow设置rootViewController,在这个rootViewController中写这两个方法并做一些其他的设置,这样控件能显示在rootViewController中,statusbar也是我们想要的状态。
上代码(demo部分代码):
// TestView.h
#import
@interface TestView : UIView
@property (nonatomic, strong) NSString *clickTitle;
- (void)show;
@end
// TestView.m
#import "TestView.h"
@implementation TestView
{
UIWindow *window;
UIButton *clickBtn;
}
- (void)makeWindow
{
window = [[UIWindow alloc] init];
window.windowLevel = UIWindowLevelStatusBar + 1;
window.backgroundColor = [UIColor clearColor];
[window makeKeyAndVisible];
TestVC *rootVC = [[TestVCalloc] init];
rootVC.view.backgroundColor = [UIColorcolorWithRed:0green:0blue:0alpha:0.5];
UINavigationController *navi = [[UINavigationControlleralloc]initWithRootViewController:rootVC];
window.rootViewController = navi;
navi.navigationBarHidden = YES;
self.frame = CGRectMake(0, 0, window.frame.size.width - 40, 80);
self.center = window.center;
[rootVC.view addSubview:self];
}
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
clickBtn = [UIButton buttonWithType:UIButtonTypeCustom];
}
returnself;
}
- (NSString *)clickTitle
{
if (!_clickTitle) {
_clickTitle = @"clicks";
}
return_clickTitle;
}
- (void)show
{
[clickBtn setTitle:self.clickTitleforState:UIControlStateNormal];
[selfmakeWindow];
}
// TestVC.m
#import "TestVC.h"
@interfaceTestVC ()
@end
@implementation TestVC
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self setNeedsStatusBarAppearanceUpdate];
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
代码很简单,却是我为了达到这一效果想了很久,感觉会有更简单的办法,以后继续研究。
以上所述是小编给大家介绍的iOS中关于UIWindow和statusbar的设置问题,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程学习网网站的支持!
本文标题为:iOS中关于UIWindow和statusbar的设置问题
- SurfaceView播放视频发送弹幕并实现滚动歌词 2023-01-02
- Flutter实现底部和顶部导航栏 2022-08-31
- Android MaterialButton使用实例详解(告别shape、selector) 2023-06-16
- 最好用的ios数据恢复软件:PhoneRescue for Mac 2023-09-14
- Android实现监听音量的变化 2023-03-30
- 作为iOS开发,这道面试题你能答出来,说明你基础很OK! 2023-09-14
- 详解flutter engine 那些没被释放的东西 2022-12-04
- Android实现轮询的三种方式 2023-02-17
- iOS 对当前webView进行截屏的方法 2023-03-01
- Android studio实现动态背景页面 2023-05-23
