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);
}
Related
Interface is define like this
#interface IGLDemoCustomView : UIView
#property (nonatomic, strong) UIImage *image;
#property (nonatomic, strong) NSString *title;
#end
While Implementation file look like this
#interface IGLDemoCustomView ()
#property (nonatomic, strong) UIImageView *imageView;
#property (nonatomic, strong) UILabel *titleLabel
#end
#implementation IGLDemoCustomView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self commonInit];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self commonInit];
}
return self;
}
- (void)commonInit
{
[self initView];
}
- (void)initView
{
self.layer.borderColor = [UIColor colorWithRed:0.18 green:0.59 blue:0.69 alpha:1.0].CGColor;
self.layer.borderWidth = 2.0;
self.layer.masksToBounds = YES;
self.backgroundColor = [UIColor whiteColor];
self.alpha = 0.8;
UIImageView *imageView = [[UIImageView alloc] init];
imageView.contentMode = UIViewContentModeCenter;
[self addSubview:imageView];
self.imageView = imageView;
UILabel *titleLabel;
self.titleLabel = [[UILabel alloc]init];
titleLabel.center = self.center; // set proper frame for label
[self addSubview:titleLabel];
}
- (void)setImage:(UIImage *)image
{
_image = image;
self.imageView.image = image;
}
- (void)setString:(NSString *)title
{
self.title=title;
self.titleLabel.text = title;
}
- (void)setFrame:(CGRect)frame
{
[super setFrame:frame];
self.imageView.frame = self.bounds;
self.layer.cornerRadius = frame.size.height / 2.0;
}
#end
When i select image from the drop down menu it shows in the menu, but when i select any text from the drop down menu it doesnt show in the drop down list view.
Any clue will be highly appreciated.
Calling and setting string in the view
IGLDropDownItem *menuButton = strongSelf.dropDownMenu.menuButton;
IGLDemoCustomView *buttonView = (IGLDemoCustomView*)menuButton.customView;
buttonView.title = device.name;
You need to set a frame for the label
- (void)setFrame:(CGRect)frame
{
[super setFrame:frame];
self.imageView.frame = self.bounds;
self.titleLabel.frame = self.bounds;
self.layer.cornerRadius = frame.size.height / 2.0;
}
If you are expecting the text to wrap, you'll need to set the lineBreakMode and set numberOfLines to 0
Like you are having UIImageView you need to have a UILabel inside your UIView too.
#interface IGLDemoCustomView ()
#property (nonatomic, strong) UIImageView *imageView;
#property (nonatomic, strong) UILabel *titleLabel;
#end
- (void)initView
{
self.layer.borderColor = [UIColor colorWithRed:0.18 green:0.59 blue:0.69 alpha:1.0].CGColor;
self.layer.borderWidth = 2.0;
self.layer.masksToBounds = YES;
self.backgroundColor = [UIColor whiteColor];
self.alpha = 0.8;
UIImageView *imageView = [[UIImageView alloc] init];
imageView.contentMode = UIViewContentModeCenter;
[self addSubview:imageView];
self.imageView = imageView;
self.titleLabel = [[UILabel alloc]init];
titleLabel.center = self.center; // set proper frame for label
[self addSubview:titleLabel]
}
And in setString
- (void)setString:(NSString *)title
{
self.title=title;
self.titleLabel.text = title;
}
I have a custom class A that is a subclass of UIView which essentially manages a UIScrollView that fits the size of the view and the contains an UIImageView that fills that scrollView.
I am having trouble setting the imageView.image of the custom class:
Here's the class
#import "BackgroundPickerView.h"
#implementation BackgroundPickerView
#synthesize scrollView;
#synthesize imageView;
#synthesize actionButton;
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
return self;
}
-(void)layoutSubviews
{
[super layoutSubviews];
//[[NSNotificationCenter defaultCenter] addObserver:self
// selector:#selector(changeImage)
// name:#"ImageChangeNotification"
// object:nil];
//Here's where we add custom subviews
scrollView = [[UIScrollView alloc] init];
imageView = [[UIImageView alloc] init];
actionButton = [[UIButton alloc] init];
[scrollView setFrame:CGRectMake(0, 0, 321, 115)];
[imageView setFrame:CGRectMake(0, 0, 321, 115)];
[actionButton setFrame:CGRectMake(0, 0, 320, 115)];
scrollView.scrollEnabled = YES;
scrollView.minimumZoomScale = 1.0;
scrollView.maximumZoomScale = 6.0;
scrollView.contentSize = CGSizeMake(300, 200);//imageView.image.size;
[scrollView addSubview:imageView];
[self addSubview:scrollView];
}
-(void)storeImage:(UIImage *)image
{
self.imageView.image = image;
}
-(void)changeImage
{
[self.imageView setImage:[UIImage imageNamed:#"random.png"]];
}
From my other class B where I receive the image I have tried using NSNotification and a simple setter method to try setting the imageView property of the class object, but cannot seem to set the imageView.image. I have tried using
instance.imageView.image = myImageToSet
//Setter
[instance storeImage:myImageToSet];
in class B but am having no success
first of all, the initialisation of subviews in layoutSubviews not the best way, better u initialise them in the init method, because layoutSubviews may call multiple times to layout the subviews, if u place the initialisation code in the layoutSubviews there might be may subviews. in layoutSubviews u only set the frame of the subviews.
and u can do like below,
#import "BackgroundPickerView.h"
#implementation CustomViewA
//Edit change below
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
[self commonInit];
}
return self;
}
- (void)awakeFromNib
{
[self commonInit];
}
- (void)commonInit
{
_scrollView = [[UIScrollView alloc] init];
_imageView = [[UIImageView alloc] init];
_actionButton = [[UIButton alloc] init];
_scrollView.scrollEnabled = YES;
_scrollView.minimumZoomScale = 1.0;
_scrollView.maximumZoomScale = 6.0;
[_scrollView addSubview:_imageView];
[self addSubview:_scrollView];
}
- (void)layoutSubviews
{
[super layoutSubviews];
[_scrollView setFrame:CGRectMake(0, 0, 321, 115)];
[_imageView setFrame:CGRectMake(0, 0, 321, 115)];
[_actionButton setFrame:CGRectMake(0, 0, 320, 115)];
_scrollView.contentSize = CGSizeMake(300, 200);
}
-(void)storeImage:(UIImage *)image
{
self.imageView.image = image;
}
-(void)changeImage
{
[self.imageView setImage:[UIImage imageNamed:#"random.png"]];
}
synthesise is optional
and in BackgroundPickerView.h file it is something like below
#interface BackgroundPickerView : UIView
#property (nonatomic, strong) UIScrollView *scrollView;
#property (nonatomic, strong) UIImageView *imageView;
#property (nonatomic, strong) UIButton *actionButton;
-(void)storeImage:(UIImage *)image;
-(void)changeImage;
for image u check it it should work, check the image is present or not,
Edit
For the image, in controller for example,
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_viewA = [[BackgroundPickerView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:_viewA];
}
//for testing
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[_viewA storeImage:[UIImage imageNamed:#"6.png"]]; //hear do 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 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
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];