UIProgressView work not correctly when setFrame in UICollectionViewCell - ios

I create a UIProgressView inside UICollectionViewCell, I try to setFrame for UIProgressView to change position in UICollectionViewCell but when do that, some cells not display progressView.
When I delete setFrame, It's OK but default width at the top of UICollectionViewCell
What's the problem?How to change UIProgressView size, origin? Please help!
//Cell:UICollectionViewCell
//Cell.m
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//ProgressView
self.progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
[self.progressView setFrame:progressViewFrame];//Some cells not display progressView
[self.progressView addSubview:self.downloadBar];
}
return self;
}

I have modified you code. Now, progress view is working properly. All cells are showing the progress view. Also, width & position of the cell can be modified, if you change the frame of self.downloadBin the below code
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self.contentView setBackgroundColor:[UIColor underPageBackgroundColor]];
//cellImage
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 0, 57, 57)];
[self.imageView setBackgroundColor:[UIColor clearColor]];
[self.contentView addSubview:self.imageView];
//ProgressView
self.downloadBar = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
[self.downloadBar setFrame:CGRectMake(0, 10, 300, 10)];
[self.contentView addSubview:self.downloadBar];
[self.downloadBar setHidden:YES];
self.receivedData = [[NSMutableData alloc] init];
}
return self;
}
I have modified the code of cell.m from the github url which you have provided.

Related

How to Call the Custom UIView from ViewController using Frame Size

I want to show the CustomUIView From my ViewController.How to call using frame?I am getting Confused in Frame as am a newbie.
My Theme is,I Want to show the LoginViewKarnataka and usernameLabel in my ViewController in the y Value 150.
This is My Code
ViewController.m
LoginViewKarnataka *loginView = [[LoginViewKarnataka alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)];
[self.view addSubview:loginView];
LoginViewKarnataka(CustomUIView)
-(instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
NSLog(#"frame==>>%f",frame);
if (self)
{
UILabel *usernameLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 20, 100, 20)];
[usernameLabel setText:#"username"];
[usernameLabel setTextColor:[UIColor blackColor]];
}
}
Change your viewController code to
LoginViewKarnataka *loginView = [[LoginViewKarnataka alloc]initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, 150)];
[self.view addSubview:loginView];
In your LoginViewKarnataka view
-(instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
[self setBackgroundColor:[UIColor redColor]];
UILabel *usernameLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 20, 100, 20)];
[usernameLabel setText:#"username"];
[usernameLabel setTextColor:[UIColor blackColor]];
[self addSubview:label];
}
return self;
}
In your above code, you are adding a label at the positions of x: 20, y: 20.
To print frame of any view use following code.
NSLog(#"frame : %#",NSStringFromCGRect(self.view.frame));
To print size of any view
NSLog(#"frame : %#",NSStringFromCGSize(self.view.frame.size));
Your code is fine. All that was missing was adding usernameLabel as a subview to your custom view.
[self addSubview:usernameLabel];
P.S. If you need to log any frame values then you can simply log the view. The frame value is printed in the description of the view. Also you can use DCIntrospect for UI debugging if you have created any complex UI.
Thanks.

rightView property error in iOS7 for UITextField

I am setting an imageView as the rightView of a textfield. This is my code:
UITextField *textField = [[UITextField alloc]initWithFrame:controlFrame];
[textField setBorderStyle:UITextBorderStyleRoundedRect];
[textField setBackgroundColor:[UIColor whiteColor]];
CGRect imageViewFrame = CGRectMake(controlFrame.origin.x + controlFrame.size.width - 20,controlFrame.origin.y, 15.0,controlFrame.size.height-10);
UIImage *image = [UIImage imageNamed:#"arrow.png"];;
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[imageView setBackgroundColor:[UIColor clearColor]];
[imageView setFrame:imageViewFrame];
[textField setRightViewMode:UITextFieldViewModeAlways];
textField.rightView = imageView;
Its working perfect on iOS 6.1 devices
But in iOS 7 devices
Any Solution for this?
Thanks in advance
Was just working on this myself and used this solution:
- (CGRect) rightViewRectForBounds:(CGRect)bounds {
CGRect textRect = [super rightViewRectForBounds:bounds];
textRect.origin.x -= 10;
return textRect;
}
This will move the image over from the right by 10 instead of having the image squeezed up against the edge in iOS 7.
Additionally, this was in a subclass of UITextField, which can be created by:
Create a new file that's a subclass of UITextField instead of the default NSObject
Add a new method named - (id)initWithCoder:(NSCoder*)coder to set the image
- (id)initWithCoder:(NSCoder*)coder {
self = [super initWithCoder:coder];
if (self) {
self.clipsToBounds = YES;
[self setRightViewMode:UITextFieldViewModeUnlessEditing];
self.leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"textfield_edit_icon.png"]];
}
return self;
}
You may have to import #import
Add the rightViewRectForBounds method above
In Interface Builder, click on the TextField you would like to subclass and change the class attribute to the name of this new subclass
There's another faster option, no need to subclass your uitextfield. There's an example
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 10, 6)];
[imageView setImage:[UIImage imageNamed:#"grey_filter.png"]];
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 6)];
[paddingView addSubview:imageView];
Just create a View bigger than your image with your padding. Add your UIImageView and then put it as left/right view in your textfield.

Custom UICellView not adding subviews

I'm using a simple custom UICellView like this:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
UIColor* color = [UIColor colorWithRed:204/255.0f green:62/255.0f blue:56/255.0f alpha:1.0f];
// Initialization code
//Styling the cell:
UIView* spacer = [[UIView alloc] initWithFrame:CGRectMake(40,200, 350, 100)];
[spacer setBackgroundColor:[UIColor colorWithRed:180/255.0f green:56/255.0f blue:61/255.0f alpha:1.0f]];
//
UIView* BlockNumber = [[UIView alloc] initWithFrame:CGRectMake(40,200, 179, 175)];
[BlockNumber setBackgroundColor:[UIColor whiteColor]];
[self.contentView addSubview:spacer];
[self.contentView addSubview:BlockNumber];
//
self.backgroundColor=color;
}
return self;
}
I tried checking the cell's subviews with a for loop, and they do seem to be there, although the won't show up inside the cell view.
However, the self.backgroundColor actually changes its color, but when it comes to adding views, it won't work.
Any help would be appreciated.
Seems like you are adding views under the cell view.
Try replacing
CGRectMake(40,200, 350, 100)
With
CGRectMake(40, 0, 350, 100)

UICollectionView with UICollectionFlowLayout not displaying cells in the right location

I have a UICollectionView in a test project that I created which does not use Interface Builder. When I run the app the test views that I give to the collection view, via the datasource, are displayed in the the top right corner around (0,0). And I cannot for the life of me figure out why. I have tried adding constraints to the cell's content view. I have also tried messing with the item insets delegate function, but that does not seem to make a difference. Am I missing something?
Here is the code for the test view controller.
#import "TestViewViewController.h"
#interface TestViewViewController () <UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>
#property (strong, nonatomic) UICollectionView *collectionView;
#property (strong, nonatomic) UICollectionViewFlowLayout *flowLayout;
#property (strong, nonatomic) NSMutableArray *testViews;
#end
#implementation TestViewViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)loadView {
self.view = [[UIView alloc] init];
self.view.backgroundColor = [UIColor whiteColor];
self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
self.testViews = [[NSMutableArray alloc] init];
UIView *testView = [[UIView alloc] init];
testView.backgroundColor = [UIColor blueColor];
testView.translatesAutoresizingMaskIntoConstraints = NO;
UILabel *testLabel = [[UILabel alloc] init];
testLabel.translatesAutoresizingMaskIntoConstraints = NO;
testLabel.text = #"I hate collection views.";
[testView addSubview:testLabel];
testView = [[UIView alloc] init];
testView.backgroundColor = [UIColor redColor];
testView.translatesAutoresizingMaskIntoConstraints = NO;
testLabel = [[UILabel alloc] init];
testLabel.translatesAutoresizingMaskIntoConstraints = NO;
testLabel.text = #"I really do.";
[testView addSubview:testLabel];
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
self.collectionView.translatesAutoresizingMaskIntoConstraints = NO;
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
self.collectionView.backgroundColor = [UIColor grayColor];
[self.view addSubview:self.collectionView];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"MyCell"];
NSDictionary *views = #{#"collectionView": self.collectionView};
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[collectionView]|" options:0 metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[collectionView]|" options:0 metrics:nil views:views]];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
#pragma mark - UICollectionView Datasource
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
return self.testViews.count;
}
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:#"MyCell" forIndexPath:indexPath];
NSLog(#"%i", self.testViews.count);
[cell.contentView addSubview: self.testViews[indexPath.row]];
return cell;
}
#pragma mark – UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
// UIView *statView = self.testViews[indexPath.row];
return CGSizeMake(100.0, 100.0);
}
#end
For openers the CGRectZero macro is the equivalent of CGRectMake(0, 0, 0, 0). It would seem that you'd be in trouble with a definition of the space in which the collection view was to be drawn and the CGRectZero origin of 0,0 would put it in the upper left corner of the view. I tried creating an app with your code - and finally got something to show up when I got the code for loadView to look like this:
- (void) loadView
{
self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
self.view.backgroundColor = [UIColor whiteColor];
self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
self.testViews = [[NSMutableArray alloc] init];
UIView* testView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, 320, 80)];
testView.backgroundColor = [UIColor blueColor];
testView.translatesAutoresizingMaskIntoConstraints = NO;
UILabel* testLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 80)];
testLabel.translatesAutoresizingMaskIntoConstraints = NO;
testLabel.text = #"I hate collection views.";
[testView addSubview:testLabel];
[self.testViews addObject:testView];
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 100, 320, 320) collectionViewLayout:self.flowLayout];
self.collectionView.translatesAutoresizingMaskIntoConstraints = NO;
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
self.collectionView.backgroundColor = [UIColor grayColor];
[self.view addSubview:self.collectionView];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"MyCell"];
}
The difference being that objects were created with a frame size - that views were added into views and so on.
My thought is that you'd be better off creating your UICollectionViewin a XIB - setting all the parameters there and also creating a subclass for UICollectionViewCell to be able to do more with the contents of the cells as need be. There's a whole lot more to be done to make this a usable UICollectionView with an arbitrary number of cells, but I have to say, the UICollectionView is not an easy object to deal with - so I sympathize with you as you come up the learning curve.

Changing Only Part of a Custom UITableViewCell Selected Background Color

I have a custom cell that I am controlling the color of when it is selected, ie the example code below:
UIView *selectedBackground = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 600, 600)];
[selectedBackground setBackgroundColor:[UIColor selectedCellColor]];
self.selectedBackgroundView = selectedBackground;
This works, however I would only like to have part of the cell change colors when selected. My custom cell is broken down into many different subviews, and I have it sectioned out where I would be able to define the specific view that I would like to change colors for.
How can I control the selectedBackgroundView, or use a different method, to have the background color change encompass a single subview in my cell?
Ya you are in the rite way,by subclassing the UITableView cell
hear is the sample code that you may find the answer for your question :)
//in subclassed cell class
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.frame = CGRectMake(0, 0, 334, 250);
UILabel *aLabel1= [[UILabel alloc]init];
UILabel *aLabel2 = [[UILabel alloc]init];
self.label1 = aLabel1;
self.label2 = aLabel2;
self.label1.text = #"Happy";
self.label2.text = #"coding";
UIImageView *bg1 = [[UIImageView alloc]init];
bg1.tag = 100;
UIImageView *bg2 = [[UIImageView alloc]init];
bg2.tag = 200;
[self addSubview:bg1]; // you must add your background views first
[self addSubview:bg2];
[self addSubview:label1];//then other views
[self addSubview:label2];
[aLabel1 release];
[aLabel2 release];
[bg1 release];
[bg2 release];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
// Configure the view for the selected state
// hear only you can manage your background views, simply i am adding 2 imageviews by setting different colors
[super setSelected:selected animated:animated];
self.backgroundColor = [UIColor greenColor];
if(selected)
{
self.label1.backgroundColor = [UIColor redColor];
self.label2.backgroundColor = [UIColor brownColor];
UIImageView *bg1 = (UIImageView *)[self viewWithTag:100];
bg1.frame = CGRectMake(0, 0,334/2, 250);
bg1.backgroundColor = [UIColor yellowColor];
}
else
{
self.label1.backgroundColor = [UIColor brownColor];
self.label2.backgroundColor = [UIColor redColor];
UIImageView *bg2 =(UIImageView *) [self viewWithTag:200];
bg2.frame = CGRectMake(35, 0, 334/2, 250);
bg2.backgroundColor = [UIColor lightGrayColor];
}
}
-(void)layoutSubviews
{
//i am setting the frame for each views that i hav added
[super layoutSubviews];
self.label1.frame = CGRectMake(10, 10, 60, 35);
self.label2.frame = CGRectMake(65, 10, 60, 35);
}
hope helps u :)
note: i am using "without ARC"

Resources