I have the following method which adds a MKNumberBadgeView to the UINavigationBar of a UITableViewController -
-(void)counterBtn{
MKNumberBadgeView *numberBadge = [[MKNumberBadgeView alloc] initWithFrame:CGRectMake(25, -10, 40, 40)];
numberBadge.strokeColor = [UIColor colorWithRed:239.0/255.0 green:117.0/255.0 blue:33/255.0 alpha:0];
numberBadge.fillColor = [UIColor colorWithRed:239.0/255.0 green:117.0/255.0 blue:33/255.0 alpha:1];
numberBadge.shine = NO;
numberBadge.hideWhenZero = YES;
numberBadge.value = _countBtnNo;
NSLog(#"Value of count = %d", _countBtnNo);
[self.navigationController.navigationBar addSubview:numberBadge];
}
All works fine until I want to change the count value - I have the following function to change the value -
- (void)removeBtn{
NSLog(#"ddd");
_countBtnNo = 0;
[self counterBtn];
}
But this does nothing - So i'm guessing I need to remove the subview first before re-adding - though there doesnt seem to be a removeSubview:numberBadge method - so i'm struggling!
Store a reference to your numberBadge in a property:
#property(nonatomic, strong) MKNumberBadgeView *numberBadge;
Then initialize it. To remove it, just call
[_numberBadge removeFromSuperview];
Alternatively you can just update the value of the counter. No need to remove it.
_numberBadge.value = new value;
Related
I have a subclass of UIView called InvitedView. It is instantiated in viewDidLoad like this:
ViewController.m
invitedView = [[InvitedView alloc] initWithFrame:CGRectMake(100, 244, 120, 80)];
invitedView.backgroundColor = [UIColor colorWithRed:156.0f/255.0f green:214.0f/255.0f blue:215.0f/255.0f alpha:0.9f];
[self.view addSubview:invitedView];
[invitedView setHidden:YES];
The class itself looks like this:
InvitedView.m
#import "InvitedView.h"
#import "AppDelegate.h"
#import "ViewController.h"
#class ViewController;
#interface InvitedView() {
UIButton *accept;
UIButton *decline;
UILabel *question;
UIView *gray;
ViewController *myViewController;
}
#end
#implementation InvitedView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
gray = [[UIView alloc] initWithFrame:frame];
NSString *holduser = [(AppDelegate*)[[UIApplication sharedApplication] delegate] invitedby];
[self addSubview:gray];
accept = [[UIButton alloc] init];
decline = [[UIButton alloc] init];
question = [[UILabel alloc] init];
question.text = [[NSString alloc] initWithFormat:#"You have been invited to a group game by %#", holduser];
question.numberOfLines = 0;
question.textAlignment = NSTextAlignmentCenter;
question.textColor = [UIColor colorWithRed:211.0f/255.0f green:243.0f/255.0f blue:219.0f/255.0f alpha:1.0f];
accept.backgroundColor = [UIColor clearColor];
accept.frame = CGRectMake(20, gray.frame.size.height / 2, (gray.frame.size.width / 2) - 10, (gray.frame.size.height / 2) - 20);
decline.backgroundColor = [UIColor clearColor];
decline.frame = CGRectMake((gray.frame.size.width / 2) + 10, (gray.frame.size.width / 2) - 20, (gray.frame.size.width / 2) - 20, (gray.frame.size.height / 2) - 20);
question.frame = CGRectMake(20, 20, gray.frame.size.width, (gray.frame.size.height / 2) - 20);
[question setFont:[UIFont fontWithName:#"HelveticaNeue-Bold" size:18.0]];
[accept addTarget:myViewController action:#selector(acceptInvite) forControlEvents:UIControlEventTouchUpInside];
[decline addTarget:myViewController action:#selector(declineInvite) forControlEvents:UIControlEventTouchUpInside];
[gray addSubview:accept];
[gray addSubview:decline];
[gray addSubview:question];
}
return self;
}
#end
The method where the view is supposed to be shown is in the view controller showing the view. It ends up getting called, I can verify that the log messages happen all the way up until the setHidden function:
ViewController.m
- (void)doSomethingWithTheNewValueOfFlagForHid {
NSLog(#"issettingtheview******");
dispatch_async(dispatch_get_main_queue(), ^(void){
NSLog(#"issettingtheviewmu2******");
[invitedView setHidden:NO];
});
}
I would like to know why invitedView isn't being shown after [invitedView setHidden:NO].
It gets all the way to setHidden, and then nothing happens. I would appreciate any help, thanks in advance.
In ViewDidLoad, change line to
[invitedView setHidden:NO];
to make sure you can actually see the view (frame is ok, no view above ...)
You might also want to check Xcodes 3D View Debugging
The only reason that it wasn't showing up, is that invitedView was being instantiated inside an if statement that wasn't being executed. However - shallowThought's idea to switch setHidden to YES started me down a more productive debugging tract, leading to the discovery.
I have a GameOver UIView that I call from inside my main UIViewController. It is just a 'popover' window that has the text game over, the score, and some blur effects to blur the main UIViewcontroller.
I try to pass an int to the UIView, but it doesn't accept it unless it is in the - (void)drawRect:(CGRect)rect method.
If I move the score label to drawRect method, the label is updated. But the blur effects go away.
What am I doing wrong?
MainViewController.m
#import "GameOverView.h"
#interface ViewController () {
GameOverView * gov;
}
- (void) showGameOver {
gov = [[GameOverView alloc] initWithFrame:self.view.bounds];
NSLog(#"Passing score of: %i", self.score);
gov.finalScore = self.score;
[self.view addSubview:gov];
}
GameOverView.h
#interface GameOverView : UIView {}
#property (nonatomic) int finalScore;
#end
GameOverView.M
#implementation GameOverView
- (id) initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
//self.backgroundColor = [UIColor redColor];
NSLog(#"Score:%i", self.finalScore );
UIVisualEffect *blurEffect;
blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView *visualEffectView;
visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
visualEffectView.frame = super.bounds;
[super addSubview:visualEffectView];
UILabel * lblGameOver = [[UILabel alloc] initWithFrame:CGRectMake(0,0, frame.size.width, 200)];
lblGameOver.center = CGPointMake(frame.size.width/2, 100);
lblGameOver.text = [NSString stringWithFormat: #"GAME OVER %i", self.finalScore];
[self addSubview:lblGameOver];
UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 200)];
button.center = CGPointMake(frame.size.width/2, 200);
[button setTitle:#"Start New Game" forState:UIControlStateNormal];
[button addTarget:self action:#selector(removeSelfFromSuperview) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
}
return self;
}
- (void) removeSelfFromSuperview{
[self removeFromSuperview];
}
You are using the finalScore property in the init method of the GameOverView class, but you are only setting its value after initializing it.
Change your initialization method to
- (id) initWithFrame:(CGRect)frame finalScore:(int)fs{
// use 'fs' instead of 'self.finalScore'
}
It should work.
I wonder how there isn't any problem with the view background color. You are initializing the view and adding it as subview like this:
gov = [[GameOverView alloc] initWithFrame:self.view.bounds];
gov.finalScore = self.score;
[self.view addSubview:gov];
This will give the view background color as black which is default color. So you don't find much difference if you use blur effect.
you need to give the color for the view during the initialization :
gov = [[GameOverView alloc] initWithFrame:self.view.bounds];
[gov setBackgroundColor:[UIColor yourColor]];
[self.view addSubview:gov];
If you are planning to keep the code in initWithFrame, you don't need to worry about setting the background color. If you keep the code in drawRect, then you must set the background color,else it will be black color.
When coming to setting the score label, it doesn't matter whether you put it in drawRect or initWithFrame method. Make sure you use drawRect method only if you really have to draw on the view,so that you can call it later by using setNeedsDisplay
I'm new to iOS development and I have an assignment where I need to implement a a UITextfield which upon a tap, brings a country selector UIPickerView.
I went through some online tutorials and got it working somehow. But it shows some strange behaviour which I can't seem to figure out.
Some strange black bars are appearing, with the picker content visibly duplicated each time I scroll.
I believe I've made a mistake in my code, forgive my lack of knowledge but can't seem to figure out what is wrong in the code.
Could you please tell me what has gone wrong?
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if ([textField isEqual:self.countryTextField]) {
countryPicker = [[UIPickerView alloc] initWithFrame:CGRectZero];
countryPicker.showsSelectionIndicator = YES;
self.countryPicker.delegate = self;
self.countryPicker.dataSource = self;
// [self.countryPicker reloadAllComponents];
countryPicker.showsSelectionIndicator = YES;
// [countryPicker init:self];
textField.inputView = countryPicker;
}
return YES;
}
- (UIView * ) pickerView: (UIPickerView * ) pickerView viewForRow: (NSInteger) row forComponent: (NSInteger) component reusingView: (UIView * ) view {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, pickerView.frame.size.width, 44)];
label.text = [NSString stringWithFormat:#"%#", [[self.countries objectAtIndex:row] objectForKey:#"name"]];
[label setTextAlignment:UITextAlignmentLeft];
[label setBackgroundColor:[UIColor clearColor]];
[label setFont:[UIFont boldSystemFontOfSize:15]];
return label;
}
Might be initialising every time , instead of initialising at every time , Check whether the UIPicker is already initialised
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if ([textField isEqual:self.countryTextField]) {
if(! countryPicker){
countryPicker = [[UIPickerView alloc] initWithFrame:CGRectZero];
countryPicker.showsSelectionIndicator = YES;
self.countryPicker.delegate = self;
self.countryPicker.dataSource = self;
countryPicker.showsSelectionIndicator = YES;
}
textField.inputView = countryPicker;
}
return YES;
}
It seems there are few things need to be changed in your code
1.Don't allocate memory to UIPickerView every time your text field get edited -textFieldShouldBeginEditing
if(self.countryPicker == nil)
{
self.countryPicker = [[UIPickerView alloc] initWithFrame:CGRectZero];
self.countryPicker.showsSelectionIndicator = YES;
self.countryPicker.delegate = self;
self.countryPicker.dataSource = self;
self.countryPicker.showsSelectionIndicator = YES;
}
2.Either use self.countrypicker -> class property or countrypicker don't use them interchangeably.
Well I finally found the issue. It was a silly mistake of defining a wrong number of components in numberOfComponentsInPickerView
Have been searching alot for this, but I think I'm doing something stupidly wrong. I have a lot of labels on my second screen, and they all have the same properties. That's why I create them via a method.
In ViewDidLoad I do this:
[self screenTwoLabelMaker:firstNameLabel withFrame:CGRectMake(30, 200, 200, 40) withText:#"First Name"];
That method is this one:
- (UILabel *)screenTwoLabelMaker:(UILabel *)sender withFrame:(CGRect)frame withText:(NSString *)text
{
sender = [[UILabel alloc] init];
sender.text = text;
sender.frame = frame;
sender.font = [UIFont systemFontOfSize:labelFontSize];
sender.textColor = [UIColor grayColor];
[self.scrollView addSubview:sender];
return sender;
}
Then I do this:
NSLog(#"firstNameLabel x: %f y:%f w:%f h:%f", firstNameLabel.frame.origin.x, firstNameLabel.frame.origin.y, firstNameLabel.frame.size.width, firstNameLabel.frame.size.height);
But the result is this:
MyApp[9608:60b] firstNameLabel x: 0.000000 y:0.000000 w:0.000000 h:0.000000
The weird thing is that the label is being put on the screen on the right place with the right text. So everything should be sunny and warm, no? Well, I also call a similar method for buttons, where there is a tag included. That tag is never active on the button (nil), so further programming for that button is killing me. For now I create them all by hand. Do you guys know what I'm doing wrong?
Your method returns your new label, so you need to assign it to the variable that you want to hold the reference. You don't need to pass in the variable to the method as the first thing you do is overwrite it. Use the following code -
firstNameLabel = [self screenTwoMakeLabelwithFrame:CGRectMake(30, 200, 200, 40) text:#"First Name"];
- (UILabel *)screenTwoMakeLabelwithFrame:(CGRect)frame text:(NSString *)text
{
UILabel newLabel = [[UILabel alloc] init];
newLabel.text = text;
newLabel.frame = frame;
newLabel.font = [UIFont systemFontOfSize:labelFontSize];
newLabel.textColor = [UIColor grayColor];
[self.scrollView addSubview:newLabel];
return newLabel;
}
I want to add SFGaugeView to my app.
When I create UIView in storyboard, change class to SFGaugeView and connect Outlet, than all works great. But... When I do it programmatically (which is what I need), it doesn't work.
Here is my progress:
#import SFGaugeView.h
Than, I've set property (#property (nonatomic) IBOutlet SFGaugeView *middleTachometer;) in .h file
and in .m file, I have this code:
self.middleTachometer = [[SFGaugeView alloc] initWithFrame:CGRectMake(0, 0, 320, 504)];
self.middleTachometer.bgColor = [UIColor colorWithRed:102/255.0 green:175/255.0 blue:102/255.0 alpha:1];
self.middleTachometer.needleColor = [UIColor colorWithRed:27/255.0 green:103/255.0 blue:107/255.0 alpha:1];
self.middleTachometer.maxlevel = 10;
self.middleTachometer.minImage = #"minImage";
self.middleTachometer.maxImage = #"maxImage";
SFGaugeView *viewC = [[SFGaugeView alloc] initWithFrame:CGRectMake(0, 0, 320, 504)];
[viewC addSubview:self.middleTachometer];
[self.view addSubview:viewC];
Result is that Gauge is in view, but it does not interact to gestures (and with storyboard, it does). Where is problem please? Thank you!
Why do you have two SFGaugeViews? I think changing your code like this:
self.middleTachometer = [[SFGaugeView alloc] initWithFrame:CGRectMake(0, 0, 320, 504)];
self.middleTachometer.bgColor = [UIColor colorWithRed:102/255.0 green:175/255.0 blue:102/255.0 alpha:1];
self.middleTachometer.needleColor = [UIColor colorWithRed:27/255.0 green:103/255.0 blue:107/255.0 alpha:1];
self.middleTachometer.maxlevel = 10;
self.middleTachometer.minImage = #"minImage";
self.middleTachometer.maxImage = #"maxImage";
[self.view addSubview:self.middleTachometer];
should do the trick!
EDIT:
Okay, got me interested - I've recreated a test project, and the problem is acutally in the SFGaugeView class - when manually adding it, the setup method is never called.
So you can either manually call the awakeFromNIB method or wait for my pull request (https://github.com/simpliflow/SFGaugeView/pull/1) to be merged in!