I need to add a UIView to the main window, the UIView consists in a view with some buttons.
The problem is, the buttons i put inside that view, is not responding for the touch events,
like it have some view in front of it.
That view need to be a singleton class, cause i need to respond the touchevent in any class.
Heres the code for the UIView :
+ (MenuBarView *)sharedMenuBar
{
static MenuBarView *sharedSingleton;
#synchronized(self) {
if (!sharedSingleton) sharedSingleton = [[MenuBarView alloc] init];
return sharedSingleton;
}
}
-(id)init
{
self = [super init];
if(self)
{
self.userInteractionEnabled = YES;
backgroundBarImage = [UIImage imageNamed:#"Barra.png"];
UIImageView * backgroundBar = [[UIImageView alloc]initWithImage:backgroundBarImage];
backgroundBar.contentMode = UIViewContentModeCenter;
backgroundBar.backgroundColor = [UIColor clearColor];
[backgroundBar setFrame:CGRectMake(0, 0, backgroundBarImage.size.width, backgroundBarImage.size.height)];
[self addSubview:backgroundBar];
UIButton * rootBTN = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[rootBTN setFrame:CGRectMake(0, 8, 100, 40)];
[rootBTN addTarget:self action:#selector(selectedBarButton:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:rootBTN];
UIImage * localizacaoIMG = [UIImage imageNamed:#"Localizador"];
UIImageView * localizacaoView = [[UIImageView alloc]initWithImage:localizacaoIMG];
localizacaoView.contentMode = UIViewContentModeScaleAspectFill;
[localizacaoView setFrame:CGRectMake(backgroundBar.frame.origin.x+130, 8, localizacaoIMG.size.width, localizacaoIMG.size.height)];
[backgroundBar addSubview:localizacaoView];
UIButton * localizacaoBTN = [UIButton buttonWithType:UIButtonTypeCustom];
[localizacaoBTN setFrame:CGRectMake(backgroundBar.frame.origin.x+110, 8, 60, 40)];
localizacaoBTN.tag = 1;
[self addSubview:localizacaoBTN];
}
return self;
}
//The event handling method
-(void)selectedBarButton:(UIButton *)sender
{
NSLog(#"OK");
[self.delegate selectedMenuBar:sender.tag];
}
and heres the implementation on the AppDelegate :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[InitialViewController alloc] init];
self.navController = [[EzMallNavViewController alloc]initWithRootViewController:self.viewController];
self.window.rootViewController = self.navController;
[self.window makeKeyAndVisible];
if(IS_IPOD)
{
[[MenuBarView sharedMenuBar]setFrame:CGRectMake(0, self.window.frame.size.height-51, [MenuBarView sharedMenuBar].frame.size.width, [MenuBarView sharedMenuBar].frame.size.height)];
}
else
{
[[MenuBarView sharedMenuBar]setFrame:CGRectMake(0, self.window.frame.size.height, [MenuBarView sharedMenuBar].frame.size.width, [MenuBarView sharedMenuBar].frame.size.height)];
}
[MenuBarView sharedMenuBar].delegate = self;
[self.window addSubview:[MenuBarView sharedMenuBar]];
return YES;
}
#pragma mark MenuBarDelegateMethods
-(void)selectedMenuBar:(int) tag
{
NSLog(#"Here");
}
It looks like your menu view has a zero size frame so no touches are detected. The buttons appear on screen because the menu view isn't set to clip drawing to its bounds.
Try to set different color for different views and analyze the view hierarchy ,it might be possible that you have added the view in wrong order .
Related
I have an 2 vc,with push from one screen to another screen.In my vc2 programmatically i am adding the nav bar title, bar button . But when i add the view some 10 points from top is reduced. not showing fully. I tried all the possibilities. Attached the image also:
in my vc2 :
- (void)viewDidLoad {
[super viewDidLoad];
_menuView.hidden = YES;
[self navItems];
}
-(void)navItems{
UIImage* filterImage = [UIImage imageNamed:#"filter.png"];
CGRect frameimg = CGRectMake(0,0, 15,15);
filterBtn = [[UIButton alloc] initWithFrame:frameimg];
[filterBtn setBackgroundImage:filterImage forState:UIControlStateNormal];
[filterBtn addTarget:self action:#selector(MenuTap:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *filter =[[UIBarButtonItem alloc] initWithCustomView:filterBtn];
self.navigationItem.rightBarButtonItem = filter;
self.title = #"Demo";
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"" style:UIBarButtonItemStylePlain target:nil action:nil];
}
- (IBAction)MenuTap:(id)sender
{
_menuView.hidden = NO;
//1
// [self.navigationController.view addSubview:_menuView];
// [self.view addSubview:_menuView];
//2
// UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
// [window addSubview: _menuView];
//
//3
// UIWindow *currentWindow = [UIApplication sharedApplication].keyWindow;
//[currentWindow addSubview:_menuView];
//4
//[[UIApplication sharedApplication].windows.lastObject addSubview:_menuView];
//5
// self.navigationController.navigationBar.layer.zPosition = -1;
//[self.view addSubview:_menuView];
}
But not able to show fully any idea please ?
based on your question in here added the detail answer, if you want to show below the status bar , then you need to get the status bar height initially, there after you need to add the y position how much you need.
- (IBAction)MenuTap:(id)sender
{
_menuView.hidden = NO;
//initially getStatusbar height
float statusBarHeight = [self statusBarHeight];
// assign your subview to window bounds
_menuView.frame = [[UIScreen mainScreen] bounds];
CGRect frameRect = _menuView.frame;
// convert your y- coordinates based on your need
frameRect.origin.y = statusBarHeight;
_menuView.frame = frameRect;
[self.navigationController.view addSubview:_menuView];
}
-(float) statusBarHeight
{
CGSize statusBarSize = [[UIApplication sharedApplication] statusBarFrame].size;
return MIN(statusBarSize.width, statusBarSize.height);
}
I am updating with #Anbu.Karthic solution.
In your (IBAction)MenuTap:(id)sender update the code with below. It will work. I have tried.
- (IBAction)MenuTap:(id)sender
{
_menuView.hidden = NO;
[self.navigationController.view addSubview:_menuView];
_menuView.frame = [[UIScreen mainScreen] bounds];
}
I was trying to add the navigation bar to the application programmatically but I didn't got any success.
I had tried the code mentioned below
UIBarButtonItem *alefttbarButton = [[UIBarButtonItem alloc] initWithTitle:#"Setting" style:UIBarButtonItemStyleBordered target:self action:#selector(settingAction:)];
alefttbarButton.style = UIBarButtonItemStyleBordered;
alefttbarButton.tintColor= [UIColor redColor];
self.navigationItem.leftBarButtonItem=alefttbarButton;
self.navigationController.delegate=self;
self.title =#"test";
Here's how I did it. In this particular case I created my views all by hand. Starting in the applicationDidFinishLaunching, I have the following code:
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
mainCtl = [[MainController alloc] init];
[window addSubview:mainCtl.nav.view];
[window makeKeyAndVisible];
As you can see I here allocate and initialize an instance of MainController, which is a subclass of UIViewController.
Then, in the initialization of this MainController I have a call to the navigation bar initialization code:
[self initNavBar];
which is the following function:
- (void) initNavBar
{
printf("initNavBar entered\n");
self.nav = [[UINavigationController alloc] initWithRootViewController: self];
//setup the middle view
int btnCount = 4;
CGRect buttonsFrame = CGRectMake(0, 0, btnCount*BUTTONWIDTH + (btnCount-1)*BUTTONSPACING + 2*BUTTONMARGIN, BUTTONHEIGHT + 2*BUTTONMARGIN);
UIView *buttonsView = [[UIView alloc] initWithFrame:buttonsFrame];
UIButton *button;
CGFloat left = BUTTONMARGIN;
// create button for photo selector invocation
button = [self buttonWithName:#"photo" target:self selector:#selector(showImagePicker) left:left];
//button = [self createButton:#"photo" action:#selector(showImagePicker) left:left];
[buttonsView addSubview:button];
left += (BUTTONWIDTH+BUTTONSPACING);
button = [self buttonWithName:#"crop" target:self selector:#selector(cropImage) left:left];
[buttonsView addSubview: button];
left += (BUTTONWIDTH+BUTTONSPACING);
kissButton = [self buttonWithName:#"kiss" target:self selector:#selector(cropImage) left:left];
[buttonsView addSubview: kissButton];
//left += (BUTTONWIDTH+BUTTONSPACING);
left += (BUTTONWIDTH+BUTTONSPACING);
button = [self buttonWithName:#"mail" target:self selector:#selector(showMailDialog) left:left];
//button = [self createButton:#"mail" action:#selector(showMailDialog) left:left];
[buttonsView addSubview:button];
Class mailClass = (NSClassFromString(#"MFMailComposeViewController"));
//only enable the mail button if the class exists
button.enabled = (mailClass != nil);
printf("initNavBar done\n");
}
This is all working code. If you can look through the cruft you'll manage to get the picture.
Hope this helps.
I have an abstract class that is a UIView subclass.
It creates a view on a new UIWindow and shows my Custom alert to users.
I have an AuthorizationView class inherited from this abstract class and it contains some UITextFields.
The problem is: these UITextFields doesn't respond on iOS 7.1 beta if I build it now and send to testers.
Old versions of project work fine (from AppStore) on iOS 7.1 beta.
Any version works fine on iOS 6.0 - iOS 7.0. I have no possibility to test it myself on iOS 7.1 beta.
Any suggestions?
UITextField creation:
fieldUser = [[UITextField alloc] initWithFrame:AUTH_FIELD_USER_FRAME];
[fieldUser setBackgroundColor:kColorClear];
[fieldUser setBorderStyle:UITextBorderStyleNone];
[fieldUser setTextAlignment:NSTextAlignmentLeft];
[fieldUser setKeyboardType:UIKeyboardTypeEmailAddress];
[fieldUser setKeyboardAppearance:[[DatabaseManager sharedInstance] keyboard]];
[fieldUser setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[fieldUser setAutocorrectionType:UITextAutocorrectionTypeNo];
[fieldUser setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[fieldUser setReturnKeyType:UIReturnKeyNext];
[fieldUser setDelegate:self];
[fieldUser setPlaceholder:NSLocalizedString(#"Window_Login_Placeholder_User", #"")];
[fieldUser setFont:kFontNormal(16.0f)];
[self addSubview:fieldUser];
Abstract class:
#import "AlertWindow.h"
#interface AlertWindow ()
{
CGFloat alpha;
}
#property (nonatomic, strong) UIWindow *window;
#end
#implementation AlertWindow
- (id) init
{
self = [super init];
if (self)
{
alpha = 0.8f;
}
return self;
}
- (id) initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
alpha = 0.8f;
}
return self;
}
- (void) showAnimated:(BOOL)animated_
{
UIView *view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[view setBackgroundColor:[UIColor blackColor]];
[view setAlpha:alpha];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.windowLevel = UIWindowLevelAlert;
self.window.backgroundColor = kColorClear;
[self.window addSubview:view];
[self.window addSubview:self];
self.window.alpha = 0.0f;
[self.window makeKeyAndVisible];
if (animated_)
{
__block UIWindow *animationWindow = self.window;
[UIView animateWithDuration:UINavigationControllerHideShowBarDuration animations:^{
animationWindow.alpha = 1.0f;
}];
}
else
{
self.window.alpha = 1.0f;
}
}
- (void) hideAnimated:(BOOL)animated_
{
if (animated_)
{
__block UIWindow *animationWindow = self.window;
[UIView animateWithDuration:UINavigationControllerHideShowBarDuration animations:^{
animationWindow.alpha = 0.0f;
} completion:^(BOOL finished) {
self.window.hidden = YES;
self.window = nil;
}];
}
else
{
self.window.hidden = YES;
self.window = nil;
}
}
- (void) hideAnimated
{
[self hideAnimated:YES];
}
- (void) setBackgroundAplha:(CGFloat)alpha_
{
alpha = alpha_;
}
#end
All the Event Delegate methods will touch the main Window of the application. Now you have created another window, may be your delegate is calling, but hiding because this new Window. Just make window color clear color and verify, or else try with printing text of Text Field entering some text, will let you know is triggered or not?
I have a UITableView which has some custom styling. This table view appears in two places in the app, one of which is inside a UIPopoverController. However when the tableview is inside the popover it takes on the default tableview styling as stated in the UI Transition Guide under "Popover".
The problem I have is that there appears to be nowhere to change this behaviour. Regardless of where I try and modify properties of the tableview the view inside the popover doesn't change.
Anyone dealt with this issue before or have any ideas?
Here is the init method of LibraryProductView where I create the table view:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.sectionOrdering = [NSArray arrayWithObjects:
[NSNumber numberWithInt:LIBRARY_PRODUCT_SECTION_DESCRIPTION],
[NSNumber numberWithInt:LIBRARY_PRODUCT_SECTION_DOCUMENTS],
[NSNumber numberWithInt:LIBRARY_PRODUCT_SECTION_ACTIVE_INGREDIENTS],
[NSNumber numberWithInt:LIBRARY_PRODUCT_SECTION_RELATED_PRODUCTS],
[NSNumber numberWithInt:LIBRARY_PRODUCT_SECTION_RELATED_DOCUMENTS], nil];
self.backgroundColor = [UIColor whiteColor];
self.tableView = [[UITableView alloc] initWithFrame:CGRectInset(self.bounds, 10, 0) style:UITableViewStyleGrouped];
self.tableView.backgroundColor = [UIColor whiteColor];
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.tableView.separatorColor = [UIColor clearColor];
self.tableView.showsVerticalScrollIndicator = NO;
[self addSubview:self.tableView];
}
return self;
}
Here is where the containing view (LibraryProductView) is added to the popover:
- (IBAction)didTouchInformationButton:(id)sender
{
if (_infoPopover != nil && _infoPopover.isPopoverVisible)
{
[_infoPopover dismissPopoverAnimated:YES];
return;
}
CGSize preferredSize = CGSizeMake(600.0f, 500.0f);
LibraryProductViewController* productController = [[[LibraryProductViewController alloc] initWithPreferredSize:preferredSize] autorelease];
productController.filterByMyCompany = NO;
productController.product = _activityInput.product;
UINavigationController* nav = [[[UINavigationController alloc] initWithRootViewController:productController] autorelease];
nav.title = _activityInput.product.name;
RELEASE(_infoPopover);
_infoPopover = [[UIPopoverController alloc] initWithContentViewController:nav];
_infoPopover.popoverContentSize = CGSizeMake(preferredSize.width, preferredSize.height + 46);
[_infoPopover presentPopoverFromRect:_infoButton.frame inView:_infoButton permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
}
The LibraryProductView is created within viewDidLoad method of LibraryProductViewController.
- (void)viewDidLoad
{
[super viewDidLoad];
self.libraryProductView = [[LibraryProductView alloc] initWithFrame:(usingPreferredSize ? CGRectMake(0.0, 0.0, preferredSize.width, preferredSize.height) : self.view.bounds)];
self.libraryProductView.dataSource = self;
self.libraryProductView.delegate = self;
[self.view addSubview:self.libraryProductView];
}
To set properties for the TableView you might do so in
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
[tableView setBackgroundColor:[UIColor redcolor]];
[tableView setSeparatorColor: [UIColor blueColor]];
return 1;
}
This, of course, assumes you have set UITableViewDataSource in your .h file
I have a singlton object. Is there any simple way to determine if current screen contains a navigation bar within singlton methods?
The singleton is UIView subclass. It's designed for showing prorgess activity, e.g. network exchange. It looks like black rectangle dropping down from top and hiding when the work is done. Why singleton? It's easy to call it from any place of code
The followed snippet is showing the initialization of activity singleton and published here just for better understaning my idea.
-(void) showUpdatingView:(NSString *) msg {
[self initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44)];
activity = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
activity.frame = CGRectMake(5, 10, 22, 22);
labelView = [[[UILabel alloc] initWithFrame:CGRectMake(35, 10, [UIScreen mainScreen].bounds.size.width - 10, 22)] autorelease];
labelView.font = [UIFont boldSystemFontOfSize:12];
labelView.backgroundColor = [UIColor clearColor];
labelView.textColor = [UIColor whiteColor];
labelView.text = msg;
[self addSubview:activity];
[self addSubview:labelView];
self.backgroundColor = [UIColor blackColor];
self.alpha = 0.7;
}
The activity can be called by
[[ActivitySingleton getInstance] showUpdatingView:#"Getting data."];
it's not all.
The singleton is being created in AppDelegate object and the view is added to
inlineActivity = [[CHInlineActivityView alloc] initView];
[self.window.rootViewController.view addSubview:inlineActivity];
I know it may look crazy. But when I was designing it seemed to me reasonable
if you have all in one navigationController:
BOOL navHidden = self.window.rootViewController.navigationController.navigatonBarHidden;
if you don't it is a bit harder.. you could check the window's subviews and see if you can find a UINavigationBar
id navbar = [self.window firstSubviewOfKind:[UINavigationBar class] withTag:NSNotFound];
BOOL navHidden = navbar == nil;
#implementation NSView (findSubview)
- (NSArray *)findSubviewsOfKind:(Class)kind withTag:(NSInteger)tag inView:(NSView*)v {
NSMutableArray *array = [NSMutableArray array];
if(kind==nil || [v isKindOfClass:kind]) {
if(tag==NSNotFound || v.tag==tag) {
[array addObject:v];
}
}
for (id subview in v.subviews) {
NSArray *vChild = [self findSubviewsOfKind:kind withTag:tag inView:subview];
[array addObjectsFromArray:vChild];
}
return array;
}
#pragma mark -
- (NSView *)firstSubviewOfKind:(Class)kind withTag:(NSInteger)tag {
NSArray *subviews = [self findSubviewsOfKind:kind withTag:tag inView:self];
return subviews.count ? subviews[0] : nil;
}
#end