TabViewController setting UINavigationBar - ios

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

Related

Adding UIButton elements to UIView programmatically do not show up

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.

Label in collection view cell won't show up

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);
}

UIView (with UIButton) on top of a UIView - how to activate button programatically?

I have a UIVIew with Button. This is loaded in the VIewController as hidden. No problem unhiding it but the button on top of it is not working. Is the UIview class the right place to put the codes for the exit button (hiding it again)?
in .h
#interface infoView : UIView
#property (nonatomic, strong) IBOutlet UIView *infoTextView;
#property (nonatomic, strong) UIButton *exitButton;
-(void)exitView:(UIButton*)sender;
#end
in .m
#import "infoView.h"
#implementation infoView
#synthesize infoTextView = _infoTextView;
#synthesize exitButton=_exitButton;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
_infoTextView = [[UIView alloc]initWithFrame:CGRectMake(40, 40, 240, 240)];
[_infoTextView setBackgroundColor: [UIColor blueColor]];
[self addSubview:_infoTextView];
_exitButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 10, 30, 30)];
//other info
[self.infoTextView addSubview:self.exitButton];
}
return self;
}
-(void)exitView:(UIButton*)sender
{
self.infoTextView.hidden=YES;
}
viewController
#implementation gjViewController
#synthesize imageViewMain = _imageViewMain;
#synthesize changeImageButton = _changeImageButton;
#synthesize infoButton = _infoButton;
#synthesize myInfo = _myInfo;
- (void)viewDidLoad
{
[super viewDidLoad];
_imageViewMain = [[UIImageView alloc] init];
//other info
[self.view addSubview:self.imageViewMain];
_changeImageButton = [[UIButton alloc] initWithFrame:CGRectMake(110, 50, 100, 50)];
//other info
[self.view addSubview:self.changeImageButton];
_infoButton = [[UIButton alloc] initWithFrame:CGRectMake(260, 30, 40, 40)];
//other details
[self.view addSubview: self.infoButton];
the section below is the part where the UIView is loaded. It loads and unhides with the IBAction at the bottom. I think you are pointing that this is wrong, not sure how. Thanks for comments.
_myInfo = [[infoView alloc] init];
[self.view addSubview:_myInfo];
[_myInfo setHidden:YES];
}
-(IBAction)newImage:(UIButton*)sender{
//task
-(IBAction)oldImage:(UIButton*)sender{
//taks
-(IBAction)viewInfo:(UIButton*)sender{ <--- unhides the UIView
[_myInfo setHidden:NO];

My ViewController's View Isn't Appearing

I have a table view controller with custom derived cells. When somebody clicks a button in the cell (the editButton below), I want to load a different view controller to display a pop up dialog.
I created a xib and view controller, and loaded the new VC that way, but everything in the new VC object was nil, and the new VC didn't appear.
I then tried programaticaly creating the view, using the initWithDefaultText:origin: initializer, but it still doesn't appear, but the member widgets are now valid.
Am I missing something?
MyListTableViewCell.h
#interface MyListTableViewCell : UITableViewCell <MyEditQueryDelegate>
...
MyListTableViewCell.m
#implementation MyListTableViewCell
+ (MyListTableViewCell *)loadFromNibWithItem:(MyListItem *)item
expandHandler:(MyItemExpandHandler)handler
controller:(MyListViewController*)controller
{
MyListTableViewCell *cell = [[[NSBundle mainBundle]
loadNibNamed:#"MyListTableViewCell" owner:self options:nil] objectAtIndex:0];
cell.item = item;
cell.expandHandler = handler;
cell.queryLabel.text = item.displayText;
[cell hideOptions];
[cell.editButton addTarget: cell action: #selector(editButtonTapped:)
forControlEvents: UIControlEventTouchUpInside];
return cell;
}
- (void) editButtonTapped: (UIButton*) sender
{
CGPoint globalOrigin = [self.superview convertPoint:self.frame.origin toView:nil];
NSLog(#"editButtonTapped Point (%f, %f).", globalOrigin.x, globalOrigin.y);
// This log message appears.
// MyEditQueryViewController* vc = [[MyEditQueryViewController alloc]
// initWithNibName:#"MyEditQueryView" bundle:nil];
// This resulted in a nil view in the vc and no visual change.
MyEditQueryViewController* vc = [[MyEditQueryViewController alloc]
initWithDefaultText:[self.queryLabel text] orign:globalOrigin];
vc.delegate = self;
[_controller presentViewController: vc animated: YES completion: nil];
// vc should appear here????
}
MyEditQueryViewController.h
#import <UIKit/UIKit.h>
#import "MyEditQueryDelegate.h"
#interface MyEditQueryViewController : UIViewController
#property (nonatomic, strong) id<MyEditQueryDelegate> delegate;
#property (nonatomic, strong) IBOutlet UITextField* textField;
#property (nonatomic, strong) IBOutlet UIView* editView;
#property (nonatomic, strong) IBOutlet UIButton* cancelButton;
#property (nonatomic, strong) IBOutlet UIButton* saveButton;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;
- (id)initWithDefaultText:(NSString*)defaultText orign:(CGPoint)origin;
- (IBAction)cancelTapped:(id)sender;
- (IBAction)saveTapped:(id)sender;
#end
MyEditQueryViewController.m
#implementation MyEditQueryViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
NSLog(#"initWithNibName:bundle:");
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
}
return self;
}
- (id)initWithDefaultText:(NSString*)defaultText orign:(CGPoint)origin
{
NSLog(#"initWithDefaultText:origin:");
self = [super initWithNibName: nil bundle:nil];
if (self)
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
self.view = [[UIView alloc] initWithFrame:screenRect];
UIImage* smokeGlass = [UIImage imageNamed:#"SmokeGlass.png"];
UIImageView* background = [[UIImageView alloc] initWithImage:smokeGlass];
[self.view addSubview:background];
CGRect editViewFrame = CGRectMake(0.0, origin.y, screenRect.size.width, 122.0);
_editView = [[UIView alloc] initWithFrame:editViewFrame];
[self.view addSubview: _editView];
CGRect textViewFrame = CGRectMake(20.0, 20.0, 280.0, 30.0);
_textField = [[UITextField alloc] initWithFrame: textViewFrame];
_textField.text = defaultText;
[_editView addSubview: _textField];
CGRect cancelButtonFrame = CGRectMake(20.0, 72.0, 50.0, 30.0);
_cancelButton = [[UIButton alloc] initWithFrame:cancelButtonFrame];
_cancelButton.titleLabel.text = #"Cancel";
[_cancelButton addTarget:self action:#selector(cancelTapped:)
forControlEvents:UIControlEventTouchUpInside];
[_editView addSubview: _cancelButton];
CGRect saveButtonFrame = CGRectMake(266.0, 72.0, 34.0, 30.0);
_saveButton = [[UIButton alloc] initWithFrame:saveButtonFrame];
_saveButton.titleLabel.text = #"Save";
[_saveButton addTarget:self action:#selector(saveTapped:)
forControlEvents:UIControlEventTouchUpInside];
[_editView addSubview: _saveButton];
}
return self;
}

Attempting to set title for views in xcode iOS project

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:#""];

Resources