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!
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 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;
How to create a simple scroll view when clicking a button in Xcode?
I have assigned the button and gave IBAction as press,
what to code to make a scroll view on this action?
-(IBAction)press
{
}
In .h file you must add delegate definition to your class declaration - something like this:
#interface someClass : NSObject <UIScrollViewDelegate>
In .m write somethink like this:
-(IBAction)press
{
UIScrollView *_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
_scrollView.backgroundColor = [UIColor clearColor];
_scrollView.userInteractionEnabled = YES;
_scrollView.delegate = nil;
[self.view addSubview:self.scrollView];
}
That's all! :)
I have included the files from the link https://github.com/cbpowell/MarqueeLabel in my project
MarqueeLabel *continuousLabel2 = [[MarqueeLabel alloc] initWithFrame:CGRectMake(10, 440, self.view.frame.size.width-20, 20) rate:10 andFadeLength:10.0f];
continuousLabel2.tag = 101;
continuousLabel2.marqueeType = MLContinuous;
continuousLabel2.animationCurve = UIViewAnimationOptionCurveLinear;
continuousLabel2.continuousMarqueeExtraBuffer = 50.0f;
continuousLabel2.numberOfLines = 1;
continuousLabel2.opaque = YES;
continuousLabel2.enabled = YES;
continuousLabel2.textAlignment = NSTextAlignmentLeft;
continuousLabel2.textColor = [UIColor colorWithRed:0.234 green:0.234 blue:0.234 alpha:1.000];
continuousLabel2.backgroundColor = [UIColor clearColor];
continuousLabel2.font = [UIFont fontWithName:#"Helvetica-Bold" size:14.000];
continuousLabel2.text = #"This is another long label that scrolls continuously with a custom space between labels! You can also tap it to pause and unpause it!"
[self.view addSubview :continuousLabel2];
I have pasted the above code in my project but the text just appears as an UILabel and not as marquee. Please advice
Put this at the end of your code
[continuousLabel2 restartLabel];
OR
[self performSelector:#selector(startlabel) withObject:nil afterDelay:1.0];
-(void)startlabel{
[continuousLabel2 restartLabel];
}
I use resetLabel to start the marquee animation.
Also don't forget to set the labelize property to NO. i.e.
[self.marqueeLabel resetLabel];
self.marqueeLabel.labelize= NO;
Courtesy of Github documentation of Marquee Label
"MarqueeLabel tries its best to automatically begin scrolling when appropriate, but sometimes the way your view/view controller appears onscreen can trip it up.
Using the restartLabel instance method to "manually" start scrolling on a MarqueeLabel"
[yourMarqueeLabel restartLabel];
I want to create a UIView subclass which will in its initializer add a UIView to its own view, like:
[self addSubview: someKindOfUIView];
This is how I've implemented it in the implementation file:
- (id)init
{
self = [super initWithFrame:CGRectMake(0, 0, 110, 110)];
if (self) {
self.grayView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
self.grayView.backgroundColor = [UIColor grayColor];
[self addSubview:self.grayView];
[self setBackgroundColor:[UIColor blueColor]];
}
return self;
}
But when I try to use instances of this class, the instances only show a bluebox, not a blue box containing a gray box. How can i fix that, is it possible? :)
Okay, after some testing and research I found the answer!
In my .h file I had a weak pointer to the grayView property:
#property (nonatomic,weak) UIView *grayView;
Instead, it should be:
#property (nonatomic,strong) UIView *grayView;
I sorta understand why, but i can't explain it in a good way, so if anyone can explain why (in an easy way) grayView has to have a strong pointer instead of a weak one, please comment under this answer ;)