UIview.hidden doesn't disappear but hides behind another view - ios

So I am trying to hide a UIView but something weird is happening. After using this line of code:
ntcCircleView.hidden = YES;
The view won't disappear, but it hides behind another UIView.
This is the complete code I use:
UIView* NtcContainer=[[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width - 40-17, 3, 40, 40)];
UIView* NtcView=[[UIView alloc]initWithFrame:CGRectMake(5, 0, 40, 40)];
notificationButton = [ZFRippleButton buttonWithType:UIButtonTypeCustom];
notificationButton.frame = CGRectMake(0, 0, 40, 40);
notificationButton.layer.cornerRadius=menuButton.frame.size.width/2;
[notificationButton addTarget:self action:#selector(goToNotificationsList:) forControlEvents:UIControlEventTouchUpInside];
notificationImage=[[UIImageView alloc]initWithFrame:CGRectMake(10, 12, 20, 20)];
notificationImage.image=[UIImage imageNamed:[HotelStay sharedInstance].icon.Notification];
ntcCircleView = [[UIView alloc] initWithFrame:CGRectMake(20,5,16,16)];
ntcCircleView.alpha = 0.7;
ntcCircleView.layer.cornerRadius = ntcCircleView.frame.size.width/2; // half the width/height
ntcCircleView.backgroundColor = [UIColor redColor];
ntcNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,16,16)];
ntcNumberLabel.textAlignment = NSTextAlignmentCenter;
[ntcNumberLabel setTextColor:[UIColor whiteColor]];
[ntcNumberLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:11.0]];
[ntcCircleView addSubview:ntcNumberLabel];
int ntcNum = [dataManager getUnreadNotificationNumber];
if (ntcNum==0)
{
ntcCircleView.hidden = YES;
}else
{
ntcNumberLabel.text = [NSString stringWithFormat:#"%i",ntcNum];
}
[NtcView addSubview:notificationImage];
[NtcView addSubview:notificationButton];
[NtcView addSubview:ntcCircleView];
[NtcContainer addSubview:NtcView];
[self.view addSubview:NtcContainer];
So ntcCircleView after hidding it, just goes behind notificationImage.
What drives me crazy, is that I use successfully the exact same code in another Views with the only difference the last line. Instead of using:
[self.view addSubview:NtcContainer];
I add the views to the navigation bar like this:
UIBarButtonItem *ntcBarItem = [[UIBarButtonItem alloc] initWithCustomView:NtcContainer];
self.navigationItem.rightBarButtonItem = ntcBarItem;
What am I missing here ?
UPDATE
I also noticed that this bug happens only when I use
[self.navigationController popViewControllerAnimated:YES];
to navigate back to the view.

You need to update these snippet:
[NtcView addSubview:notificationImage];
[NtcView addSubview:notificationButton];
[NtcView addSubview:ntcCircleView];
[NtcContainer addSubview:NtcView];
to:
[NtcView addSubview:notificationButton];
[NtcView addSubview:ntcCircleView];
[NtcView addSubview:notificationImage];
[NtcContainer addSubview:NtcView];
It will solve your problem.

I figured it out. I was using a deprecated method (viewDidUnload) to release the notification observer.

Related

Cannot get UIButton to programmatically call action in iOS

I cannot get my button to fire the associated method. I am not using IB or anything like that (all programmatic). The _mainViewController is passed in during custom init, just to clarify (although this should have nothing to do with the button). Please see my code snips:
viewcontroller.h
#import <UIKit/UIKit.h>
#interface CSNWZSSViewController : UIViewController
- (void)doneButtonPressed;
#property UIViewController *mainViewController;
#property UIButton *doneButton;
#end
viewcontroller.m
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _mainViewController.view.frame.size.width, 45)];
headerView.backgroundColor = [UIColor darkGrayColor];
_doneButton = [[UIButton alloc] initWithFrame:CGRectMake(_mainViewController.view.frame.size.width - 100, 0, 100, headerView.frame.size.height)];
[_doneButton addTarget:self
action:#selector(doneButtonPressed)
forControlEvents:UIControlEventTouchUpInside];
[_doneButton setTitle:#"Done" forState:UIControlStateNormal];
_doneButton.tintColor = [UIColor whiteColor];
_doneButton.backgroundColor = [UIColor colorWithRed:0 green:.77 blue:0 alpha:1];
[headerView addSubview:_doneButton];
CGRect headerLabelFrame = CGRectMake(5, 5, headerView.frame.size.width - 105, headerView.frame.size.height - 5);
UILabel *headerLabel = [[UILabel alloc] initWithFrame:headerLabelFrame];
headerLabel.text = #"test header";
headerLabel.textColor = [UIColor whiteColor];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.font = [UIFont systemFontOfSize:20.0f];
headerLabel.lineBreakMode = NSLineBreakByTruncatingTail;
[headerView addSubview:headerLabel];
[self.view addSubview:headerView];
}
- (void)doneButtonPressed
{
NSLog(#"button pressed");
}
Update 10/9/16 using buttonWithType init and explicit userInteractionEnabled
_doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_doneButton addTarget:self
action:#selector(doneButtonPressed)
forControlEvents:UIControlEventTouchUpInside];
[_doneButton setTitle:#"Done" forState:UIControlStateNormal];
_doneButton.tintColor = [UIColor whiteColor];
_doneButton.backgroundColor = [UIColor blackColor]; //[UIColor colorWithRed:0 green:.77 blue:0 alpha:1];
_doneButton.frame = CGRectMake(_mainViewController.view.frame.size.width - 100, 0, 100, headerView.frame.size.height);
_doneButton.userInteractionEnabled = YES;
[headerView addSubview:_doneButton];
Note 12/9/16 the _mainViewController object is passed in here:
CSNWZSSViewController *CSNWZSSVC = [[CSNWZSSViewController alloc] initWithRichTextString:source withTitleString:title forViewController:self.viewController];
[self.viewController.view addSubview:CSNWZSSVC.view];
Solution with matt's help (see below) The context was getting messed up due to the lack of adding the new view controller.
CSNWZSSViewController *CSNWZSSVC = [[CSNWZSSViewController alloc] initWithRichTextString:source withTitleString:title forViewController:self.viewController];
[self.viewController addChildViewController:CSNWZSSVC];
[self.viewController.view addSubview:CSNWZSSVC.view];
The _mainViewController is passed in during custom init
I'm going to guess that this is the problem — that you are mismanaging this view controller and that it is not being correctly made part of the view controller hierarchy, and in fact may even be going out of existence so that there is no one to send the button action message to.
edit Yes, now that you've posted more code, I'm clearly right:
CSNWZSSViewController *CSNWZSSVC = [[CSNWZSSViewController alloc] initWithRichTextString:source withTitleString:title forViewController:self.viewController];
[self.viewController.view addSubview:CSNWZSSVC.view];
That is totally wrong behavior. You cannot arbitrarily add another view controller's view to yours. There is a strict dance for managing a child view controller, and you are not doing the dance.
Try:
UIButton *theButton = [UIButton buttonWithType:UIButtonTypeCustom];
And then set your frame.

titleView jumps after segue

In my app I have a UINavigationController with a UISplitViewController as rootViewController. The masterViewController of this UISplitViewController sets a UIView with a UITextField centered inside it as titleView. This is wat it looks like in the storyboard.
- (void)viewDidLoad {
[super viewDidLoad];
self.titleView = [[UIView alloc] initWithFrame:CGRectMake((self.view.frame.size.width-((self.view.frame.size.width/2)+150))/2, 0, (self.view.frame.size.width/2)+150, 30)];
self.rightButton = [[UIButton alloc] init];
[self.rightButton setImage:newImage forState:UIControlStateNormal];
[self.rightButton addTarget:self action:#selector(barButtonRight:) forControlEvents:UIControlEventTouchUpInside];
self.rightButton.frame = CGRectMake(0, 0, 30, 30);
self.rightButton.tintColor = [UIColor whiteColor];
[self.rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:self.rightButton];
self.splitViewController.navigationItem.rightBarButtonItem = rightButton;
self.textField = [[UITextField alloc]init];
if (IDIOM == IPAD) {
self.textField.frame = CGRectMake(0, 0, self.titleView.frame.size.width, 30);
} else {
self.textField.frame = CGRectMake(65, 0, (self.view.frame.size.width-20)-110, 30);
}
self.textField.backgroundColor = [UIColor purpleColor];
if (IDIOM == IPAD) {
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, 20)];
self.textField.leftView = paddingView;
self.textField.leftViewMode = UITextFieldViewModeAlways;
}
[self.titleView addSubview:self.textField];
self.splitViewController.navigationItem.titleView = self.titleView;
[self.navigationController setNavigationBarHidden:YES animated:NO];
}
Now, when a the right button is pressed a strange thing happens.
- (IBAction)barButtonRight:(id)sender {
[self performSegueWithIdentifier:#"showDetail" sender:self];
}
The UITextField is pushed all the way to the left inside the UIView. I commented out most of my viewDidLoad code and this is still happening. Any ideas as to what's causing this?

Button Text not showing

When I try to post the following code, my app just shows an empty rectangle where the button's text should be. I am a beginner at xcode and I have determined this code from other tutorials.
- (void)viewDidLoad {
[super viewDidLoad];
UILabel *player1 = [[UILabel alloc]initWithFrame:CGRectMake(35, 120,130, 30)];
[player1 setText:#"Player"];
player1.layer.borderWidth = 1.0;
player1.layer.borderColor = [UIColor blackColor].CGColor;
player1.textAlignment = UITextAlignmentCenter;
[self.view addSubview: player1];
UIButton *score1 = [[UIButton alloc]initWithFrame:CGRectMake(165, 120, 60, 30)];
[score1 setTitle:#"0" forState:UIControlStateNormal];
score1.layer.borderWidth = 1.0;
score1.layer.borderColor = [UIColor blackColor].CGColor;
[self.view addSubview: score1];
UILabel *player2 = [[UILabel alloc]initWithFrame:CGRectMake(35, 150, 130, 30)];
[player2 setText:#"Opponent"];
player2.layer.borderWidth = 1.0;
player2.layer.borderColor = [UIColor blackColor].CGColor;
player2.textAlignment = UITextAlignmentCenter;
[self.view addSubview: player2];
}
Like I said, everything but the text in the button is showing up.
The reason why the button looks blank is because its text color is actually white. Just give it a different color and 0 will now show:
score1.layer.backgroundColor = [UIColor blackColor].CGColor;
If you don't want to change the button background color, you may want to set the text color:
[score1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
Read Question: [initWithFrame:frame] vs. [UIButton buttonWithType], for more about creating a UIButton with these different methods.
The problem is the way you're creating the button:
UIButton *score1 = [[UIButton alloc]initWithFrame:CGRectMake(165, 120, 60, 30)];
That's not how you do it. Do it like this:
UIButton *score1 = [UIButton buttonWithType:UIButtonTypeSystem];
score1.frame = CGRectMake(165, 120, 60, 30);
All will be well after that!

How can I change size of UINavigationBar in UINavigationViewController in iOS 8?

What is the proper way to change height of navigation bar?
I need to create custom title in navigation bar, it should contains two UILabels one above other. The Title should be resized to fit those UILabels.
Should I override sizeThatFits: method in my custom TitleView, would other buttons correctly change to fit that size? How can I change a size of NavigationBar?
That is what I need to create using latest SDK features.
Create the following class category (you can create it in your implementation file):
#import "objc/runtime.h"
#interface UINavigationBar (CustomHeight)
- (void)setHeight:(CGFloat)height;
#end
static char const *const kHeight = "Height";
#implementation UINavigationBar (CustomHeight)
- (void)setHeight:(CGFloat)height
{
objc_setAssociatedObject(self, kHeight, #(height), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSNumber *)height
{
return objc_getAssociatedObject(self, kHeight);
}
- (CGSize)sizeThatFits:(CGSize)size
{
CGSize newSize;
if (self.height) {
newSize = CGSizeMake(self.superview.bounds.size.width, [self.height floatValue]);
} else {
newSize = [super sizeThatFits:size];
}
return newSize;
}
#end
And after that, simply call [self.navigationController.navigationBar setHeight:100.0] in your viewDidLoad or where you need it. Works in both iOS 7.1 and 8.1.
Disclaimer: Any alteration of the API and its functions is prone to future issues with new OS releases! Apple does not intend to allow us to change the navigation bar height (except for some rare instances) so use any solution wisely after assessing the risks vs advantages.
Looking at that design that you posted, I don't have the impression that the bar is really higher than the standard 44pt. Please note that starting with iOS7 the status bar is integrated into the same bar and the 20pt of the status bar is added to the total height.
IMHO, the only problem you need to solve is to stack the title/subtitle, and that can be easily done with a custom titleView, as Kampai has demonstrated.
Add custom view for both navigation title and navigation right bar button.
Here how you can add title view like above.
// Custom view for navigation title.
UIView *customTitleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 22)];
label1.text = #"Stasik";
label1.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:20];
label1.textAlignment = NSTextAlignmentCenter;
[customTitleView addSubview:label1];
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 22, 100, 22)];
label2.text = #"Stasik iPhone";
label2.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:16];
label2.textAlignment = NSTextAlignmentCenter;
label2.textColor = [UIColor darkGrayColor];
[customTitleView addSubview:label2];
self.navigationItem.titleView = customTitleView;
// Custom view for right navigation.
UIButton *upButton = [UIButton buttonWithType:UIButtonTypeCustom];
upButton.frame = CGRectMake(0, 0, 20, 44);
[upButton setImage:[UIImage imageNamed:#"up"] forState:UIControlStateNormal];
[upButton addTarget:self action:#selector(upButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *upButtonBarItem = [[UIBarButtonItem alloc] initWithCustomView:upButton];
UIButton *downButton = [UIButton buttonWithType:UIButtonTypeCustom];
downButton.frame = CGRectMake(0, 0, 20, 44);
[downButton setImage:[UIImage imageNamed:#"down"] forState:UIControlStateNormal];
[downButton addTarget:self action:#selector(downButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *downButtonBarItem = [[UIBarButtonItem alloc] initWithCustomView:downButton];
// Remove trailing space for right view.
UIBarButtonItem *nagativeSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
nagativeSpace.width = -11;
self.navigationItem.rightBarButtonItems = #[nagativeSpace, upButtonBarItem, downButtonBarItem];
// Left item
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0, 0, 40, 44);
[backButton setImage:[UIImage imageNamed:#"back"] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(backButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
// Remove leading space for left view.
nagativeSpace.width = -15;
UIBarButtonItem *backButtonBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItems = #[nagativeSpace, backButtonBarItem];
OutPut:
Arrow images are different than OP's requirement but it serves layout for navigation views.
From above code make change in nagativeSpace.width value to arrange buttons with accurate distance from left as well as right margins.

How can I know which view I am clicking? And why the button no reaction?

I am designing a viewcontroller which have several button bar, each bar canbe clicked and show a content view.like:
http://i.stack.imgur.com/m5V4Q.png
When I click the buttonbar, it's frame will expand bigger, and you can see the content in it(which is a button).
First, a bar button(320*30 size) and a contentView is a set, they are subviews of listview. And several listview makes the whole view.
Second, when I click it, the list view will expand from 320*30 to 320*180, the contentview in it will expand from 300*0 to 300 * 130(20 pixel of margin). and I set the clipsToBounds of contentview to Yes to make sure the content in contentview won't show when the frame height is 0.
Now,the click can show the contentview exactly as I want. But here is a problem: I can't click the button in it, and I tried to set their userInteractionEnabled to yes . And even I set the contentview as user-defined,which I set the view's userInteractionEnabled to yes and overwrite the touchbegin function to show a alertView. Those test are failed, no reaction found. And I checked and sure that there shouldn't be any view covered it.
What's the reason might be?
The code of setup the list view is:
NSArray *array = [titleArray objectAtIndex:i];
NSString *ShenSuoTitle = [array objectAtIndex:0];
NSString *ShenSuoContent = [array objectAtIndex:1];
UIView *listView = [[UIView alloc] initWithFrame:CGRectMake(0, totalHeight, 320, ShenSuoViewHeight)];
[listView setBackgroundColor:[UIColor blackColor]];
[listView setTag:[[NSString stringWithFormat:#"%d",(i+1)] intValue]];
[self.scrollView addSubview:listView];
totalHeight = totalHeight + 1 + ShenSuoViewHeight;
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, ShenSuoViewHeight)];
[titleView setBackgroundColor:[UIColor whiteColor]];
[titleView setTag:[[NSString stringWithFormat:#"%d1",i+1] intValue]];
[listView addSubview:titleView];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTag:[[NSString stringWithFormat:#"%d2",i+1] intValue]];
[btn setFrame:CGRectMake(0, 0, 320, ShenSuoViewHeight)];
[btn addTarget:self action:#selector(reSetFrame:) forControlEvents:UIControlEventTouchUpInside];
[titleView addSubview:btn];
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(5, 12, 30, 30)];
img.image = [UIImage imageNamed:#"list_ico.png"];
[img setTag:[[NSString stringWithFormat:#"%d3",i+1] intValue]];
[titleView addSubview:img];
NSLog(#"img:%f,%f",img.frame.origin.y,img.frame.size.height);
UILabel *labTitle = [[UILabel alloc] initWithFrame:CGRectMake(35, 15, 100, 25)];
labTitle.textColor = [UIColor blackColor];
labTitle.backgroundColor = [UIColor clearColor];
labTitle.font = [UIFont boldSystemFontOfSize:15];
labTitle.text = ShenSuoTitle;
[titleView addSubview:labTitle];
NSLog(#"labTitle:%f,%f",labTitle.frame.origin.y,labTitle.frame.size.height);
//add a label for selected
UILabel *selectedLabel = [[UILabel alloc] initWithFrame:CGRectMake(214, 14, 86, 21)];
selectedLabel.textColor = [UIColor grayColor];
//selectedLabel.alpha = 0.75;
selectedLabel.backgroundColor = [UIColor clearColor];
selectedLabel.text = #"All";
selectedLabel.font = [UIFont boldSystemFontOfSize:15];
[selectedLabel setTag:[[NSString stringWithFormat:#"%d5",i+1] intValue]];
[titleView addSubview:selectedLabel];
NSLog(#"selectedLabel:%f,%f",selectedLabel.frame.origin.y,selectedLabel.frame.size.height);
UIView *content = [[UIView alloc] initWithFrame:CGRectMake(10, 10 + ShenSuoViewHeight, 300, 0)];
content.backgroundColor = [UIColor grayColor];
UILabel *testLa = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
testLa.text = #"Label";
UIButton *testBut = [UIButton buttonWithType:UIButtonTypeRoundedRect];
testBut.frame = CGRectMake(0, 40, 100, 30);
testBut.titleLabel.textColor = [UIColor greenColor];
testBut.titleLabel.text = #"Button";
content.tag = [[NSString stringWithFormat:#"%d4",i+1] intValue];
[content addSubview:testLa];
[content addSubview:testBut];
content.clipsToBounds = YES;
[testLa release];
[listView addSubview:content];
[content release];
[labTitle release];
[img release];
[titleView release];
[listView release];
the code handle the click to make the list view expands is:
UIView *titleView = (UIView *)[self.view viewWithTag:[[NSString stringWithFormat:#"%d1",i+1] intValue]];
titleView.backgroundColor = [UIColor colorWithRed:0.92 green:0.92 blue:0.92 alpha:0.9];
UIView *listView = (UIView *)[self.view viewWithTag:[[NSString stringWithFormat:#"%d",i+1] intValue]];
listView.frame = CGRectMake(0, totalHeight, 320, ShenSuoViewHeight);
totalHeight = totalHeight + 1 + 20 + 180 + ShenSuoViewHeight;
UIView *content = [self.view viewWithTag:[[NSString stringWithFormat:#"%d4",i+1] intValue]];
content.frame = CGRectMake(10, 10 + ShenSuoViewHeight, 300, 180);
UIImageView *img = (UIImageView*)[self.view viewWithTag:[[NSString stringWithFormat:#"%d3",i+1] intValue]];
img.image = [UIImage imageNamed:#"list_ico_d.png"];
I'm not sure I follow your situation exactly, and some code would be nice. To answer your question though, the reason is most likely that the touch is being intercepted elsewhere. It may also be that you just forgot to connect an action method to the button. Try using breakpoints or NSLogs to see what methods are being triggered and which aren't.
Thanks a lot. It is due to my fault. The code of expanding the bar which setting the listview's frame is wrong, it should add in the expanded height. This leads the visual is ok but the clicking not be handled.
So this problem closed.

Resources