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:#""];
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
}
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.
I'm writing a custom UIView class that has its own sub UIView. I want this UIView to be centered inside the superView with two UIButtons inside of it, two UILabels.
I overwrite my init with the parameters I want in the custom UIView and in that init method in implementation, I am alloc/initializing all my objects and at the end of my init method, I add my subView to self. And I add all the buttons and objects to the subView.
The problem is, even after setting my coordinates and frames in layoutSubviews, my buttons and labels do not appear in the subView.
Here's my method code:
#property (nonatomic, weak) UIView *alertSubView; // this will be the message view in middle of screen
#property (nonatomic, weak) UILabel *alertTitleLabel; // This will be the title within the alertSubView
#property (nonatomic, weak) UILabel *alertMessageLabel; // This will be the alert message under the alert title
#property (nonatomic, weak) UIButton *doNotAskButton; // This will be the do not ask button
#property (nonatomic, weak) UIButton *cancelButton; // This will be the cancel button, bottom left of alertView
#property (nonatomic, weak) UIButton *confirmButton; // This will be the confirm button, bottom right of alertView
#property (nonatomic) BOOL doNotAsk;
#end
#implementation MyAlertView
- (instancetype)initWithFrame:(CGRect)frame messageTitle:(NSString *)title messageSubject:(NSString *)subject shouldAskForDoNotAsk:(BOOL)doNotAsk delegate:(id<MyAlertViewDelegate>)delegate
{
if (self = [super initWithFrame:frame]) {
self.delegate = delegate;
UILabel *alertLabel = [[UILabel alloc] init];
alertLabel.text = title;
alertLabel.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:15];
self.alertTitleLabel = alertLabel;
UILabel *messageLabel = [[UILabel alloc] init];
messageLabel.text = subject;
alertLabel.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:15];
self.alertMessageLabel = messageLabel;
self.backgroundColor = [UIColor colorWithRed:170.0f/255.0f green:170.0f/255.0f blue:170.0f/255.0f alpha:0.75];
UIView *alertBoxView = [[UIView alloc] init];
self.alertSubView = alertBoxView;
[self.alertSubView addSubview:self.alertTitleLabel];
[self.alertSubView addSubview:self.alertMessageLabel];
if (doNotAsk) {
UIButton *buttonDoNotAsk = [[UIButton alloc] init];
buttonDoNotAsk.titleLabel.text = #"Do not ask me again";
self.doNotAskButton = buttonDoNotAsk;
[self.alertSubView addSubview:self.doNotAskButton];
}
UIButton *cancelButton = [[UIButton alloc] init];
cancelButton.titleLabel.text = #"Cancel";
cancelButton.titleLabel.textColor = [UIColor blueColor];
cancelButton.backgroundColor = [UIColor whiteColor];
cancelButton.opaque = YES;
self.cancelButton = cancelButton;
[self.alertSubView addSubview:self.cancelButton];
UIButton *confirmButton = [[UIButton alloc] init];
confirmButton.titleLabel.text = #"OK";
self.confirmButton = confirmButton;
[self.alertSubView addSubview:self.confirmButton];
self.alertSubView.backgroundColor = [UIColor whiteColor];
[self addSubview:self.alertSubView];
}
return self;
}
- (void)layoutSubviews
{
// place the alertView in the center of self view
CGFloat alertHeight = kAlertHeightModifier * self.frame.size.height;
CGFloat alertWidth = kAlertWidthModifier * self.frame.size.width;
[self.alertSubView setFrame:CGRectMake(0, 0, alertWidth, alertHeight)];
[self.alertSubView addSubview:self.cancelButton];
[self setUpButtonsAndLabels];
[self.alertSubView setCenter:self.center];
}
- (void) setUpButtonsAndLabels {
CGFloat alertHeight = kAlertHeightModifier * self.frame.size.height;
CGFloat alertWidth = kAlertWidthModifier * self.frame.size.width;
CGFloat buttonWidth = 0.5 * alertWidth;
CGFloat buttonHeight = 0.2 * alertHeight;
[self.cancelButton setFrame:CGRectMake(15, 45, buttonWidth, buttonHeight)];
[self.confirmButton setFrame:CGRectMake(0, 0, buttonWidth, buttonHeight)];
[self.confirmButton setCenter:self.center];
}
Try replacing your - (void)layoutSubviews with this .
- (void)layoutSubviews
{
// place the alertView in the center of self view
CGFloat alertHeight = kAlertHeightModifier * self.frame.size.height;
CGFloat alertWidth = kAlertWidthModifier * self.frame.size.width;
[self.alertSubView setFrame:CGRectMake(0, 0, alertWidth, alertHeight)];
[self.alertSubView addSubview:self.cancelButton];
[self setUpButtonsAndLabels];
[self.alertSubView setCenter:self.center];
[super layoutSubviews];
}
Or else initialize the button in viewdidload .It will work .
Try doing it in viewDidLoad. Should help to eliminate the problem, because the view will be fully initialized and ready to use.
I'm trying yo have a label and image show up in collection view cells, but only the image will here's my code
The header File
#import <UIKit/UIKit.h>
#import "ComicMetadata.h"
#interface ComicCell : UICollectionViewCell
#property (nonatomic, retain) UIImageView *page;
#property (nonatomic, retain) UILabel *name;
#end
The Body
#import "ComicCell.h"
#implementation ComicCell
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self){
self.page = [[UIImageView alloc] init];
self.name = [[UILabel alloc] initWithFrame:self.bounds];
self.autoresizesSubviews = YES;
self.name.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
self.name.font = [UIFont systemFontOfSize:13];
self.name.textAlignment = NSTextAlignmentLeft;
[self.contentView addSubview:self.page];
[self.contentView addSubview:self.name];
self.name.text = #"Test";
}
return self;
}
- (void)layoutSubviews{
[super layoutSubviews];
//self.page.frame = self.contentView.bounds;
self.page.frame = CGRectMake(0, 0, 234, 312);
}
What am I doing wrong? extra text because Stackoverflow doesn't like the code to text ratio
When ComicCell init with frame, the bounds does not init exactly, you must set frame for the label again in layoutSubviews.
- (void)layoutSubviews{
[super layoutSubviews];
self.page.frame = CGRectMake(0, 0, 234, 312);
self.name.frame = CGRectMake(0, 0, 234, 312);
}
I am trying to programatically setup my UINavigationBar. I have setup the NavBar for each tab in interface builder and it inherits from my GenericTabUiNavBar in interface builder.
However when I run the app, the GenericTabUiNavBar is not loaded with its elements into the UINavBar.
Here is my GenericTabUiNavBar code:
.h
#import <UIKit/UIKit.h>
#interface GenericTabUiNavBar : UINavigationBar
#property (nonatomic, strong) UILabel *firstLetter;
#property (nonatomic, strong) UILabel *secondLetter; //need two labels
#property (nonatomic,strong) UIButton *leftSideButton;
#property (nonatomic,strong) UIButton *rightSideButton;
#end
.m
#implementation GenericTabUiNavBar
#synthesize firstLetter;
#synthesize secondLetter;
#synthesize leftSideButton;
#synthesize rightSideButton;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
// Initialization code
//r g b
//Yellow Color = 255,219,17
UIColor *yellowColor = [[UIColor alloc] initWithRed:255 green:219 blue:17 alpha:1];
UIColor *blueColor = [[UIColor alloc] initWithRed:1 green:129 blue:200 alpha:1];
UIFont *fontFaceAndSize = [UIFont fontWithName:#"HelveticaNeue" size:36];
//self
self.frame = CGRectMake(0, 20, 320, 44);
self.backgroundColor = blueColor;
self.barTintColor = blueColor;
//x.108 y.31
//w.15 / y.21
CGRect firstLetterRect = CGRectMake(108, 31, 15, 21);
self.firstLetter = [[UILabel alloc] initWithFrame:firstLetterRect];
firstLetter.text = #"An";
firstLetter.font = fontFaceAndSize;
firstLetter.textColor = [UIColor whiteColor];
//128 31
//22 21
CGRect secLetterRect = CGRectMake(108, 31, 15, 21);
self.secondLetter = [[UILabel alloc] initWithFrame:secLetterRect];
secondLetter.text = #"APP";
secondLetter.font = fontFaceAndSize;
secondLetter.textColor = yellowColor;
//tex pos =
self.leftSideButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 29, 30, 26)];
self.rightSideButton = [[UIButton alloc] initWithFrame:CGRectMake(207, 29, 30, 26)];
[leftSideButton setImage:[UIImage imageNamed:#"anImg.png"] forState:UIControlStateNormal];
[rightSideButton setImage:[UIImage imageNamed:#"otherImg.png"] forState:UIControlStateNormal];
[self addSubview:leftSideButton];
[self addSubview:firstLetter];
[self addSubview:secondLetter];
[self addSubview:rightSideButton];
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end
In my TabViewController I call it by hooking it up in interface builder. Why does this not load?
When controllers are loaded from storyboard or xib. The init method
- (id)initWithCoder:(NSCoder *)decoder
is called instead of the
- (instancetype)initWithNibName:(NSString *)nibName
bundle:(NSBundle *)nibBundle
as for views the reason might be the same