Hi How can i create a custom UISlider like the image below.
I googled a lot still couldn't able to find a way to do that. Can anybody suggest me how to achieve this. Any help would be greatly appreciated.
Custom UISlider
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController :UIViewController
{
}
#property (strong, nonatomic) IBOutlet UILabel *LineLbl;
#property (strong, nonatomic) IBOutlet UIScrollView *scrollviewObj;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
{
}
#end
#implementation ViewController
#synthesize LineLbl;
#synthesize scrollviewObj;
- (void)viewDidLoad
{
[super viewDidLoad];
UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(10,LineLbl.frame.origin.y + LineLbl.frame.size.height +20, self.view.frame.size.width -20, 20)];
slider.maximumValue = 1;
slider.value = 0.6;
[slider setMinimumTrackTintColor:[UIColor clearColor]];
[slider setMaximumTrackTintColor:[UIColor clearColor]];
[slider setThumbImage:[UIImage imageNamed:#"sliderImg"] forState:UIControlStateNormal];
CAGradientLayer *red_gradient =[CAGradientLayer layer];
UIColor *startColor=[UIColor yellowColor];
UIColor *midColor=[UIColor colorWithRed:(155/255.f) green:(250/255.f) blue:(90/255.f) alpha:1];
UIColor *endColor= [UIColor colorWithRed:(96/255.f) green:(255/255.f) blue:(59/255.f) alpha:1];
red_gradient.frame = slider.bounds;
red_gradient.colors = [NSArray arrayWithObjects:(id)[startColor CGColor], (id)[midColor CGColor],(id)[endColor CGColor], nil];
[red_gradient setStartPoint:CGPointMake(0.0, 0.5)];
[red_gradient setEndPoint:CGPointMake(1.0, 0.5)];
[slider.layer insertSublayer:red_gradient atIndex:2];
[scrollviewObj addSubview: slider];
}
I followed this.
Related
I'd like to be able to customize any buttons I'll create within a specific style of my app. I was trying to create a new class which will consist of all my settings, so every time I add a new button I can use methods of the class to customize the new button.
I'm a very new to the iOS programming so I quess I might've been doing something wrong, but that's what I've got so far:
I created the class with settings
#import "CustomButton.h"
#implementation CustomButton
-(UIButton *) setButtonWithType{
self.layer.cornerRadius = 25;
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowOffset = CGSizeMake(1.5f, 1.5f);
self.layer.shadowOpacity = 1.0f;
self.layer.shadowRadius = 0.0f;
self.layer.masksToBounds = NO;
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.bounds;
gradient.startPoint = CGPointMake(0.5, 0.5);
gradient.endPoint = CGPointMake(0.0, 0.5);
gradient.colors = [NSArray arrayWithObjects:(id)[UIColor whiteColor].CGColor,(id)[UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1.0].CGColor, nil];
gradient.cornerRadius = self.layer.cornerRadius;
[self.layer insertSublayer:gradient atIndex:0];
return self;
}
#end
ViewController.h
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIButton *button;
#end
ViewController.m
#import "ViewController.h"
#import "CustomButton.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
CustomButton *btn = [[CustomButton alloc] init];
self.button = [btn setButtonWithType];
//rest of the code
Any ideas how to make it works?
Or do I need to use a very different approach?
Thank you!!
You may find success doing this as a Category (lots of examples out there)...
UIButton+ShadowRoundedGradient.h
#import <UIKit/UIKit.h>
#interface UIButton (ShadowRoundedGradient)
- (void) makeMe;
#end
UIButton+ShadowRoundedGradient.m
#import "UIButton+ShadowRoundedGradient.h"
#implementation UIButton (ShadowRoundedGradient)
- (void) makeMe {
self.layer.cornerRadius = 25;
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowOffset = CGSizeMake(1.5f, 1.5f);
self.layer.shadowOpacity = 1.0f;
self.layer.shadowRadius = 0.0f;
self.layer.masksToBounds = NO;
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.bounds;
gradient.startPoint = CGPointMake(0.5, 0.5);
gradient.endPoint = CGPointMake(0.0, 0.5);
gradient.colors = [NSArray arrayWithObjects:(id)[UIColor whiteColor].CGColor,(id)[UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1.0].CGColor, nil];
gradient.cornerRadius = self.layer.cornerRadius;
[self.layer insertSublayer:gradient atIndex:0];
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
#property (weak, nonatomic) IBOutlet UIButton *button;
}
#end
ViewController.m
#import "ViewController.h"
#import "UIButton+ShadowRoundedGradient"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.button makeMe];
//rest of the code
}
My custom UIView is not triggering this to run, but other custom views I make trigger it. Does anyone know the reason that this automatic refreshing of views would be triggered? Or Why "Refresh All Views" is greyed out.
Code is in here:
https://github.com/HannahCarney/HCRotaryWheel
Make sure you have IBInspectable properties for your view. (like
changing your view color,just to make sure your IBDesignable thing
works).
Make sure you have the code for your customisation in your view .m
file. (in your view draw rect method).
Make sure you provide the file owner name for your IBDesignable view.(in your storyboarD)
Make sure you mention IB_DESIGNABLE on your view .h file.
Finally a qucik clean and build of your storyboard,shall enable the
option of Refresh All views.
Sample code:
for view.h
#import <UIKit/UIKit.h>
IB_DESIGNABLE
#interface designalble_UIView : UIView
#property (nonatomic) IBInspectable UIColor *startColor;
#property (nonatomic) IBInspectable UIColor *midColor;
#property (nonatomic) IBInspectable UIColor *endColor;
#property (nonatomic) IBInspectable NSInteger borderWidth;
#property (nonatomic) IBInspectable CGFloat cornerRadious;
#property (nonatomic) IBInspectable BOOL isHorizontal;
#end
for view.m
#import "designalble_UIView.h"
#implementation designalble_UIView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.startColor = [UIColor redColor];
self.midColor = [UIColor greenColor];
self.endColor = [UIColor blueColor];
self.borderWidth = 2;
self.cornerRadious = 10;
self.isHorizontal = NO;
[self customInit];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self customInit];
}
return self;
}
- (void)drawRect:(CGRect)rect {
[self customInit];
}
- (void)setNeedsLayout {
[super setNeedsLayout];
[self setNeedsDisplay];
}
- (void)prepareForInterfaceBuilder {
[self customInit];
}
- (void)customInit {
self.layer.cornerRadius = self.cornerRadious;
self.layer.borderWidth = self.borderWidth;
if (self.cornerRadious > 0) {
self.layer.masksToBounds = YES;
}
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[self.startColor CGColor],(id)[self.midColor CGColor], (id)[self.endColor CGColor], nil];
gradient.endPoint = (self.isHorizontal) ? CGPointMake(1, 0) : CGPointMake(0, 1);
[self.layer insertSublayer:gradient atIndex:0];
}
I solved this because I had put IB_DESIGNABLE in the wrong spot in my code:
I had a protocol at the top of my view and previously it looked like
IB_DESIGNABLE
#protocol RotaryProtocol <NSObject>
- (void) wheelDidChangeValue:(int)currentSector;
#end
#interface HCRotaryWheel : UIView
#property (weak) id <RotaryProtocol> delegate;
#property (nonatomic, strong) IBInspectable UIColor* background;
#end
To fix this I had to move IB_DESIGNABLE below protocol
#protocol RotaryProtocol <NSObject>
- (void) wheelDidChangeValue:(int)currentSector;
#end
IB_DESIGNABLE
#interface HCRotaryWheel : UIView
#property (weak) id <RotaryProtocol> delegate;
#property (nonatomic, strong) IBInspectable UIColor* background;
#end
Now my views refresh!!
I'm trying to instante a simple UIButton from a UIView already created in Interface Builder, but nothing's happening.
I've attached the following codes to the view:
"DetailViewTwo.h":
#import <UIKit/UIKit.h>
#interface DetailViewTwo : UIView
#property (nonatomic) bool viewIsShown;
#property (nonatomic) float lastY;
#property (weak, nonatomic) IBOutlet UILabel *titleLabel;
#property (strong, nonatomic) NSMutableArray *radioArray;
-(void)createOption:(NSString *)legenda Value:(NSString *)value action:(SEL)action;
-(void)clearView:(NSString *)novoNome;
-(void)escolherRota:(id)sender;
#end
"DetailViewTwo.m":
#import "DetailViewTwo.h"
#import "MainViewController.h"
#import <QuartzCore/QuartzCore.h>
#implementation DetailViewTwo
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_viewIsShown=false;
[self.layer setMasksToBounds:YES];
self.layer.cornerRadius=6;
UIButton *but=[UIButton buttonWithType:UIButtonTypeRoundedRect];
but.frame= CGRectMake(100, 15, 15, 15);
[but setTitle:#"Ok" forState:UIControlStateNormal];
[but addTarget:self action:#selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:but];
}
return self;
}
#end
(Oh, also, my 'self.layer.cornerRadius=6;' is not working either, is it related??)
If you have this view created in storyboard you should use
-initWithCoder: initialiser instead of -initWithFrame:.
Also you should add the button to self.view not superview.
- (id)initWithCoder:(NSCoder *)aDecoder {
if(self = [super initWithCoder:aDecoder]) {
//Your code goes here
}
}
You need to add UIButton to View
[self.superview addSubview:but];
should be
[self addSubview:but];
i have a solution to instantiate (add) button in view. As per your code, you just need to replace [self.superview addSubview:but]; with this [self.view addSubview:but];.
UIButton *but=[UIButton buttonWithType:UIButtonTypeRoundedRect];
but.frame= CGRectMake(100, 15, 15, 15);
[but setTitle:#"Ok" forState:UIControlStateNormal];
[but addTarget:self action:#selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:but]; // Changes done
This is firstPopoverViewController.h code:
#import <UIKit/UIKit.h>
#interface firstPopoverViewController : UIViewController
#end
This is my firstPopoverViewController.m code:
#import "firstPopoverViewController.h"
#interface firstPopoverViewController ()
#end
#implementation firstPopoverViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.contentSizeForViewInPopover = CGSizeMake(300, 290);
// Header label
UILabel *h1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 85)];
h1.font = [UIFont fontWithName:#"myFont" size:22.0];
h1.textColor = [UIColor blackColor];
h1.textAlignment = NSTextAlignmentCenter;
h1.text = #"Heading";
h1.numberOfLines = 0;
h1.backgroundColor = [UIColor greenColor];
// Ok button BG View
UIView *buttonBG = [[UIView alloc] initWithFrame:CGRectMake(0, 300-75, 300, 75)];
buttonBG.backgroundColor = [UIColor greenColor];
// Ok button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(300/2-130/2, 290-35-15, 130, 35);
button.backgroundColor = [UIColor whiteColor];
[button setTitle:#"OK" forState:UIControlStateNormal];
[button addTarget:self action:#selector(closePop) forControlEvents:UIControlEventTouchUpInside];
button.adjustsImageWhenHighlighted=YES;
// Adding views
[self.view addSubview:h1];
[self.view addSubview:buttonBG];
[self.view addSubview:button];
}
-(void)closePop {
}
#end
Then there's ViewController.h:
#import <UIKit/UIKit.h>
#import "firstPopoverViewController.h"
#interface ViewController : UIViewController
#property (strong, nonatomic) UIButton *popButton;
#property (strong, nonatomic) firstPopoverViewController *firstPopoverViewController;
#property (strong, nonatomic) UIPopoverController *firstPopover;
#end
And finally ViewController.m:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
/* ##### UIPopController stuff ##### */
UIImage *popButtonImage = [UIImage imageNamed:#"menu.png"];
_popButton = [UIButton buttonWithType:UIButtonTypeCustom];
_popButton.frame = CGRectMake(0, 0, 73, 66);
[_popButton addTarget:self action:#selector(openPop) forControlEvents:UIControlEventTouchUpInside];
_popButton.adjustsImageWhenHighlighted=NO;
[_popButton setImage:popButtonImage forState:UIControlStateNormal];
[self.view addSubview:_popButton];
}
-(void)openPop {
if (_firstPopoverViewController == nil) {
//Create the _firstPopoverViewController.
_firstPopoverViewController = [[firstPopoverViewController alloc] init];
}
if (_firstPopover == nil) {
_firstPopover = [[UIPopoverController alloc] initWithContentViewController:_firstPopoverViewController];
_firstPopover.popoverContentSize = CGSizeMake(300, 290);
[_firstPopover presentPopoverFromRect:CGRectMake(0,0, 73, 66) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
NSLog(#"show");
} else {
NSLog(#"dismiss");
[_firstPopover dismissPopoverAnimated:YES];
_firstPopover = nil;
}
}
#end
It's pretty basic code that displays a button and when I click this button it shows popover. I want to close this popover using button that's inside firstPopoverViewControll.m file. There's a closePop{} method, what should I put inside it to close this popover? Thanks.
By the way I'm beginner as you can see, I researched stackoverflow and there are some solutions with delegates, which seems to be working for others, but didn't work for me, could you please show me a solution on my code that I posted? Thank you people very much.
There may be a simpler method that I'm not aware of, but the following method should work:
Use NSNotificationCenter to post a notification back to the ViewController containing the UIPopOverController to tell it to dismiss the popover.
First, in ViewController.m viewDidLoad add:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(closePop:) name:#"ClosePopOver" object:nil];
Then add the following method to ViewController.m:
- (void)closePop:(NSNotification *)notification {
[_firstPopover dismissPopoverAnimated:YES];
}
Then in irstPopoverViewController.m:
- (void)closePop {
[[NSNotificationCenter defaultCenter] postNotificationName:#"ClosePopOver" object:nil];
}
That should do the trick.
A delegate is the way to go. I do admit though that I was confused by them at first, but they are quite simple to setup.
In your firstPopoverController.h put this:
#import <UIKit/UIKit.h>
#protocol FirstPopoverDelegate
- (void) closedPopover;
#end
#interface firstPopoverViewController : UIViewController
#property (nonatomic, assign) id< FirstPopoverDelegate > delegate;
#end
Then in your .m of you popover,
-(void)closePop
{
[self.delegate closedPopover];
}
In your main UIViewController's .h:
#import <UIKit/UIKit.h>
#import "firstPopoverViewController.h"
#interface ViewController : UIViewController <FirstPopoverDelegate>
#property (strong, nonatomic) UIButton *popButton;
#property (strong, nonatomic) firstPopoverViewController *firstPopoverViewController;
#property (strong, nonatomic) UIPopoverController *firstPopover;
#end
Then in the .m, first register to listen of the delegate by adding this to your openPop method:
this is important and easy to forget.. nothing will happen if it is not set
_firstPopoverViewController.delegate = self;
Finally, in your .m add the delegate method:
- (void)closedPopover
{
//you can also pass data back in this function, just modify its parameters here and when you define it in the .h of the popover
[_firstPopoverViewController dismissPopoverAnimated:YES];
}
What do I need to do to be able to set the title of each main view in my iOS project. Below is what I've got so far and what I'm attempting to do...
Created Files That I'm attempting to get to work together.
PageTitleView.h
PageTitleView.m
PageBaseViewController.h
PageBaseViewController.m
HomeViewController.h
HomeViewController.m
ActivityFeedViewController.h
ActivityFeedViewController.m
Essentially setup the PageTitleView.h * .m along with PageBaseViewController.h & .m so that I can then make a call into the HomeViewController&ActivityFeedViewController and set the title of the current view.
Here is my code from those files
PageTitleView.h
#import "ContainerView.h"
#import "BaseLabel.h"
#interface PageTitleView : ContainerView
#property (nonatomic, strong) BaseLabel* label;
#property (nonatomic, strong) NSString* title;
#end
PageTitleView.m
#import "PageTitleView.h"
#implementation PageTitleView
#synthesize label;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, 35)];
label = [[BaseLabel alloc] initWithFrame:CGRectMake(18, 10, frame.size.width , 25)];
label.font=[UIFont fontWithName:#"Helvetica" size:15.0];
label.textColor = (UIColor *) COLOR_DARK_BLUE;
label.text =[[NSString alloc] initWithFormat: #"text"];
self.backgroundColor = (UIColor *)COLOR_WHITE;
self.layer.shadowOpacity = 0.05;
self.layer.shadowOffset = CGSizeMake(.25, .25);
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowRadius = 5;
self.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.layer.bounds].CGPath;
self.layer.cornerRadius = 10.0;
self.layer.borderWidth = 0.25;
self.layer.borderColor = [UIColor grayColor].CGColor;
[self addSubview:label];
return self;
}
#end
PageBaseViewController.h
#import <QuartzCore/QuartzCore.h>
#import <UIKit/UIKit.h>
#import "Settings.h"
#import "CustomButton.h"
#import "PageTitleView.h"
#interface PageBaseViewController : UIViewController
#property (nonatomic, strong) UIScrollView *scrollView;
#property (nonatomic, strong) CustomButton *backButton;
#end
PageBaseViewController.m
#import "PageBaseViewController.h"
#interface PageBaseViewController ()
#end
#implementation PageBaseViewController
synthesize scrollView;
- (void)viewDidLoad {
[super viewDidLoad];
// GRADIENT
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.view.bounds;
gradient.colors = [NSArray arrayWithObjects:
(id)BACKGROUND_GRADIENT_COLOR_1.CGColor,
(id)BACKGROUND_GRADIENT_COLOR_2.CGColor,
(id)BACKGROUND_GRADIENT_COLOR_3.CGColor, nil];
gradient.locations = [NSArray arrayWithObjects:
(id)[NSNumber numberWithFloat:0.0],
(id)[NSNumber numberWithFloat:0.4],
(id)[NSNumber numberWithFloat:1.0], nil];
[self.view.layer insertSublayer:gradient atIndex:0];
float height = self.view.frame.size.height - TITLEBAR_HEIGHT - self.tabBarController.tabBar.frame.size.height;
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, TITLEBAR_HEIGHT, self.view.frame.size.width, height)];
[self.view addSubview:scrollView];
PageTitleView *pageTitle = [[PageTitleView alloc] initWithFrame:CGRectMake(105, -10, 100 , 35)];
[self.view addSubview:pageTitle];
/* This button is currently added to the view and will show in the TITLEBAR_HEIGHT.
These will be used throughout the entire project, we will be working next to have
correct buttons turn on and off for each view.*/
CustomButton *buttonLeft = [[CustomButton alloc] initWithFrame:CGRectMake(5, 5, 60, 25) andText:#"Left"];
[buttonLeft addTarget:self action:#selector(buttonLeft:) forControlEvents:UIControlEventTouchUpInside];
[buttonLeft setBackgroundColor:BUTTON_COLOR_1];
[self.view addSubview:buttonLeft];
// Remove this button from homeview and have a logo show up.
CustomButton *buttonRight = [[CustomButton alloc] initWithFrame:CGRectMake(235, 5, 80, 25) andText:#"Right"];
[buttonRight addTarget:self action:#selector(buttonRight:) forControlEvents:UIControlEventTouchUpInside];
[buttonRight setBackgroundColor:BUTTON_COLOR_1];
[self.view addSubview:buttonRight];
}
- (void)buttonLeft:(UIButton *)buttonLeft {
NSLog(#"Ping");
}
- (void)buttonRight:(UIButton *)buttonRight {
NSLog(#"Ping");
}
#end
Solved this with the following steps.
Imported the PageTitleView.h into the PageBaseViewController.h
In the PageBaseViewController.h declared the following property
#property (nonatomic, strong) PageTitleView *pageTitle;
Then in the PageBaseViewController adjusted the import of PageTitleView
From:
PageTitleView *pageTitle = [[PageTitleView alloc] initWithFrame:CGRectMake(105, -10, 100 , 35)];
To:
pageTitle = [[PageTitleView alloc] initWithFrame:CGRectMake(105, -10, 100 , 35)];
Then in one of my views for example HomeViewController I added the following code.
[self.pageTitle setTitle:#"Home"];
Now I'll add the following code to my other views and fill it in with the appropriate name.
[self.pageTitle setTitle:#""];