I am trying to add a view on top of the statusbar. I have been following this SO post: Adding view on StatusBar in iPhone
For some reason, when I create the window and set Hidden to NO, the view does not appear to show up on top of the statusbar. Does this implementation still work in ios5.1?
Thanks!
This is my custom UIWindow class:
#implementation StatusBarOverlay
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Place the window on the correct level and position
self.hidden = NO;
self.windowLevel = UIWindowLevelStatusBar+1.0f;
self.frame = [[UIApplication sharedApplication] statusBarFrame];
// Create an image view with an image to make it look like a status bar.
UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:self.frame];
backgroundImageView.image = [UIImage imageNamed:#"bar_0.png"];
backgroundImageView.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:#"bar_1.png"],
[UIImage imageNamed:#"bar_2.png"],
[UIImage imageNamed:#"bar_3.png"],
nil];
backgroundImageView.animationDuration = 0.8;
[backgroundImageView startAnimating];
[self addSubview:backgroundImageView];
}
return self;
}
In my viewcontroller, I created the new window and called this in viewDidLoad:
StatusBarOverlay *overlayWindow = [[StatusBarOverlay alloc] initWithFrame:CGRectZero];
[overlayWindow makeKeyAndVisible];
However, the view still doesn't show up. Any idea as to why?
set your application's window level to UIWindowLevelStatusBar:
self.window.windowLevel = UIWindowLevelStatusBar;
and then add your own view to application window anywhere:
[[[UIApplication sharedApplication]delegate].window addSubview:yourview];
this problem came to me recently, and I just solved it through this way
You can create a new UIWindow and add your view to that. Set the windows frame to [[UIApplication sharedApplication] statusBarFrame] and call makeKeyAndVisible to make it visible.
In the end, I subclassed UIWindow and made that the primary UIWindow in the application AppDelegate. I added any custom view and set the window level to UIWindowLevelStatusBar to display on top of the status bar.
Related
i have set the custom color to navigation bar of QLPreviewController
but the problem is that i want dark color of navigation bar in QLPreviewController
even i have set the navigation bar translucent property to No
But i Dont know Why it is not working
I want it like belowed image
but it shows like this image
QLPreviewController *previewer = [[QLPreviewController alloc] init];
// Set data source
[previewer setDataSource:self];
[previewer setDelegate:self];
// Which item to preview
[previewer setCurrentPreviewItemIndex:index];
[previewer.view addGestureRecognizer:singleTap];
previewer.navigationController.navigationBar.translucent = NO;
previewer.navigationController.navigationBar.barTintColor = [UIColor redColor];
self.navigationController.navigationBar.translucent=NO;
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
[self.navigationController pushViewController:previewer animated:TRUE ];
even i have also tried like this but it is also not working
- (id <QLPreviewItem>)previewController: (QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
// Break the path into it's components (filename and extension)
// Use the filename (index 0) and the extension (index 1) to get path
//lblFileName.text=[strFileName stringByReplacingOccurrencesOfString:#"movefile" withString:#""];
// For navigation bar color and text attributes of navigation bar
for (id object in controller.childViewControllers)
{
if ([object isKindOfClass:[UINavigationController class]])
{
UINavigationController *navController = object;
navController.navigationBar.translucent=NO;
navController.navigationBar.barTintColor = [UIColor redColor];;
navController.toolbar.translucent=NO;
navController.toolbar.barTintColor=[UIColor redColor];;
[navController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil]];
}
}
NSString *strFilename=[[NSBundle mainBundle]pathForResource:#"final" ofType:#"png"];
return [NSURL fileURLWithPath:strFilename];
}
Please suggest me where i am going wrong
Thanks In Advance
The main problem is that when you try to set the translucency of the navigation bar, you haven't pushed the preview controller on the navigation stack yet.
At this point, the preview controller is allocated and instantiated, but its view has not been loaded or added to the view hierarchy, and the value of previewer.navigationController is nil. The value of self.navigationController is not nil at this point, but the translucency property you set here will be overwritten as a side effect of pushing the preview controller. The easiest way to get the effect you want, is to swap the order of the statements, like this:
[self.navigationController pushViewController:previewer animated:YES];
self.navigationController.navigationBar.translucent = NO;
Do note that with the translucency of the navigation bar set to NO, the previewed content will start under the navigation bar, which is probably not what you want. The easiest way to work around that issue, is to set the translucency property after the view of the preview controller has appeared on screen. You can do this by subclassing QLPreviewController:
#interface PreviewController : QLPreviewController
#end
#implementation PreviewController
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.navigationController.navigationBar.translucent = NO;
}
Note that things get more complicated when you present the preview controller modally (rather than pushing it on a navigation stack). In that case, there is no navigation controller available to access the navigation bar and you need to rely on the internal view hierarchy of the QLPreviewController. The following code works in iOS7, but might break in a later version:
[self presentViewController:previewController animated:YES completion:^{
UIView *view = [[[previewController.view.subviews lastObject] subviews] lastObject];
if ([view isKindOfClass:[UINavigationBar class]])
{
((UINavigationBar *)view).translucent = NO;
}
}];
No need for subclassing. try this:
QLPreviewController * ql = [[QLPreviewController alloc] init];
...
[self showViewController:ql sender:self];
ql.navigationController.navigationBar.translucent = NO;
ql.edgesForExtendedLayout = UIRectEdgeNone;
If you want to present it modally, just put the previewController into a navigationController:
UINavigationController * navi = [[UINavigationController alloc] initWithRootViewController:ql];
ql.navigationController.navigationBar.translucent = NO;
ql.edgesForExtendedLayout = UIRectEdgeNone;
[self presentViewController:navi animated:YES completion:nil];
Instead of using QLPreviewController,y don't you use webview to load the pdf.But if you load pdf in webview you need to customise the buttons thats it.Its also look very similar to QLPreviewController.
NSURL *targetURL = [NSURL fileURLWithPath:path_pdf]; //path_pdf is pdf's path
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webview_pdf loadRequest:request];
Well,try it once.
If Still, anyone wants to customize the Navigation bar of QLPreviewController then they can try creating a UIViewController and customize it as per requirement and then create QLPreviewController object and add it as a child view controller.
This will allow you to get rid of share button and you customize navigation bar color etc. this is working for me.
To know, how to add child view controller you can refer to this
[[[UINavigationBar appearance] setTitleView:button]
I tried to set title view as button for all navigation item using above line of code but it is not working.How can I set title view for all navigation bars in my project by writing small code in appdelegate ?
Customizing the Appearance of a Navigation Bar
it is alright to modify the barStyle, tintColor, and translucent
properties, but you must never directly change UIView-level properties
such as the frame, bounds, alpha, or hidden properties directly.
For more detail you can follow apple doc UINavigationBar Class Reference
Edit ->
I solved your query
[[UINavigationBar appearance] addSubview:yourView];
Other Navigation Related query
try this dude...
//put whatever object in this view..for example button that you want to put...
UIView* ctrl = [[UIView alloc] initWithFrame:navController.navigationBar.bounds];
ctrl.backgroundColor = [UIColor yellowColor];
ctrl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[navController.navigationBar addSubview:ctrl];
let me know it is working or not!!!!
Happy Coding!!!
This is how you create a navigation controller in addDelegate and set a title to it,
you have to add the following code to didFinishLaunchingWithOptions method.
Notice that you need to set a root view for the viewController which will the viewController that will be displayed inside the navigationController.
yourViewController *mainVC = [[yourViewController alloc]init];
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:mainVC];
navigationController1.title = #"title";
[window addSubview:navigationController1.view];
If you use iOS4, you can override drawRect
#implementation UINavigationBar (UINavigationBarCategory)
-(void)drawRect:(CGRect)rect
{
UIImageView *itleImageView = //create object
[self addSubView:titleImageView];
//set title view in navigation bar
}
#end
iOS 5,
if ([[UIDevice currentDevice].systemVersion floatValue] >= 5.0) {
if ([self.navigationController.navigationBar respondsToSelector:#selector( setBackgroundImage:forBarMetrics:)]){
UIImageView *itleImageView = //create object
[self.navigationController.navigationBar addSubView:titleImageView];
}
}
i guess this is a right, as i don't have implemented.
I wanted to add a logo in every NavigationController, so I made a subclass of
UINavigationController and used this in the viewDidLoad-Method
UIImage *logo =[UIImage imageNamed:#"logo"];
CGFloat navWidth = self.navigationBar.frame.size.width;
CGFloat navHeight = self.navigationBar.frame.size.height;
UIImageView *logoView = [[UIImageView alloc] initWithFrame:CGRectMake(navWidth-logo.size.width - 5,
(navHeight - logo.size.height) / 2,
logo.size.width,
logo.size.height)];
logoView.image = logo;
[self.navigationBar addSubview:logoView];
I've just started learning how to develop an iPhone app.
I'm trying to make an app with two switches. I made two classes (Switch1 & Switch2).
First, I tested the app with one switch (Switch1), and the app worked. But when I made the second class (Switch2) and I Build/Run the app, the first switch (Switch1) disappeared, and what I saw just the second switch (Switch2).
After that I made the background of the (Switch1 & Switch2) celarColor, I could see both of switches. However, the first switch (Switch1) can't be switched.
so I think my problem is how to make both switches (Switch1 & Switch2) visible and working at the same time in the "window"
The question (could be stupid): What can I make them visible and working at the same time?
I think the problem in the following code: This is from the AppDelegate
UIScreen *s1 = [UIScreen mainScreen];
view1 = [[Switch1 alloc] initWithFrame: s1.applicationFrame];
window = [[UIWindow alloc] initWithFrame: s1.bounds];
[window addSubview: view1];
[window makeKeyAndVisible];
UIScreen *s2 = [UIScreen mainScreen];
view2 = [[Switch2 alloc] initWithFrame: s2.applicationFrame];
window = [[UIWindow alloc] initWithFrame: s2.bounds];
[window addSubview: view2];
[window makeKeyAndVisible];
return YES;
Here is the Switch1.h
#import
#interface Switch1 : UIView {
UISwitch *mySwitch1;
}
#property (nonatomic, retain) IBOutlet UISwitch *mySwitch1;
#end
Here is the Switch1.m
#import "Switch1.h"
#implementation Switch1
#synthesize mySwitch1;
- (id) initWithFrame: (CGRect) frame {
if ((self = [super initWithFrame: frame])) {
// Initialization code
self.backgroundColor = [UIColor clearColor];
mySwitch1 = [[UISwitch alloc] initWithFrame: CGRectZero];
if (mySwitch1 == nil) {
[self release];
return nil;
}
mySwitch1.on = NO; //the default
[mySwitch1 addTarget: [UIApplication sharedApplication].delegate
action: #selector(valueChanged:)
forControlEvents: UIControlEventValueChanged
];
CGRect b1 = self.bounds;
mySwitch1.transform = CGAffineTransformMakeScale(2, 2);
mySwitch1.center = CGPointMake(
b1.origin.x + b1.size.width / 2,
b1.origin.y + b1.size.height / 2
);
[self addSubview: mySwitch1];
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void) drawRect: (CGRect) rect {
// Drawing code
}
*/
- (void) dealloc {
[mySwitch1 release];
[super dealloc];
}
#end
You might want to configure a view controller with a view and then toss your two switches on that first. Do you understand the MVC patterns here: https://developer.apple.com/library/ios/#documentation/General/Conceptual/DevPedia-CocoaCore/MVC.html
and here's the view controller guide: https://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html.
When you're initializing with UIScreen, you're making both switches the same size (the window size) and thus switch 2 is over switch 1 since it's initialized second.
So you are adding one screen (s2) on top of another screen (s1) and therefore you are not able to access s1. You need to make the size of s2 and s1 smaller so that they do not take the whole screen size.
Also by saying makeKeyAndVisible you make the window visible and able to accept user interaction. No need to say that twice.
You're resetting the window when you call
window = [[UIWindow alloc] initWithFrame: s2.bounds];
s1 isn't there anymore because you've created a new window over it. You could just do
UIScreen *s1 = [UIScreen mainScreen];
window = [[UIWindow alloc] initWithFrame: s1.bounds];
view1 = [[Switch1 alloc] initWithFrame: s1.applicationFrame];
view2 = [[Switch2 alloc] initWithFrame: s2.applicationFrame];
[window addSubview: view1];
[window addSubview: view2];
[window makeKeyAndVisible];
return YES;
If you're just starting, definitely check out this tutorial on iPhone dev. It shows how to use UIViewController, UIView, and a lot of the supplied classes for the iPhone like UITableView and UIImageView.
I have a xib with a subview that I set to Class: S7GraphView (if I select it and click on the identity inspector)
When all I have in the (void) didViewLoad { ...
s7graphView = [[S7GraphView alloc] initWithFrame:[self.view bounds]];
//[self.view addSubview:s7graphView];
s7graphView.dataSource = self;
...
}
In the nib, I control-hold the file's owner to the subview and select - S7GraphView and it still shows up empty in the simulator.
If I comment all this out, I get a black subview (I do set the background to black) but not graph:
// s7graphView = [[S7GraphView alloc] initWithFrame:[self.view bounds]];
// //[self.view addSubview:s7graphView];
// s7graphView.dataSource = self;
// [s7graphView release];
If I remove the subview from my view and instead add it programmatically, it works when I add it like this:
s7graphView = [[S7GraphView alloc] initWithFrame:CGRectMake(20., 70., 280.0, 240.0 )];
[self.view addSubview:s7graphView];
s7graphView.dataSource = self; [s7graphView release];
I should be able to add it via the xib too, right? What do youz think I'm doing wrong?
Thanks!!
Have you implemented the delegate and the datasource?
I have used an actionsheet in my project and when it appears it show all buttons but last (4th) button does not responds to my click(only it's half part responds)..
I know the reason it is because i have used a TabBarController and the present class is inside that tabbar controller....
only that part of the actionsheet is responding which is above the tabs....and my last button is half above and half is on top of tabbar
please help
i suggest using this:
[actionSheet showInView:[UIApplication sharedApplication].keyWindow];
I had the same problem that you have and using this method to show it worked for me. The TabBar wants to stay key Window what makes your bottom button appear above, but is actually under the tabbar.
Hope this does the trick for you..
Edit
If you use landscape mode and the method above doesn't work. You can use the following fix:
#Vinh Tran: [sheet showFromTabBar:self.parentViewController.tabBarController.tabBar]
What method do you use to show your actionsheet. Try showFromTabBar: method
The real problem comes in, when your interface is rotated to landscape and the parent view controller has a transformation on it. Believe me, that's a realistic scenario, doh. Then the action sheet is clipped and you can't use the parentViewController because it is transformed. The solution to avoid all these issues is to create a new window, add a rotatable view controller as rootViewController and use its view to display the sheet.
CGRect applicationRect = [[UIScreen mainScreen] bounds];
UIWindow* actionSheetWindow = [[UIWindow alloc] initWithFrame:applicationRect];
RotationViewController* rootViewController = [[RotationViewController alloc] initWithNibName:nil bundle:nil];
actionSheetWindow.rootViewController = rootViewController;
[rootViewController release];
actionSheetWindow.hidden = NO;
UIActionSheet* actionSheet = [[UIActionSheet alloc] initWithTitle:nil];
[actionSheet setCancelButtonWithTitle:#"Cancel" handler:^{
actionSheetWindow.hidden = YES;
[actionSheetWindow release];
}];
[actionSheet showInView:rootViewController.view];
The code above uses BlocksKit, but you can do it also by using the actionSheet delegate and instance properties.
RotationViewController is just a UIViewController subclass that implements
- (void) viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor clearColor];
self.view.opaque = NO;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}