I had a rounded UIButton in a custom table view cell initialized via a XIB, and it was working fine. I just noticed that the border, a circular pink border, now doesn't appear. I tried adding some code to also set the property in the awakeFromNib function, but this also didn't do the trick.
- (void)awakeFromNib {
[super awakeFromNib];
[self setupProfileView];
self.contentView.backgroundColor = [UIColor colorWithWhite:0.97f alpha:1.0f];
self.selectionStyle = UITableViewCellSelectionStyleNone;
_initialLabel.layer.cornerRadius = _initialLabel.frame.size.width/2;
_initialLabel.layer.borderWidth = 2;
_initialLabel.layer.borderColor = [UIColor colorWithRed:223.0/255.0 green:158.0/255.0 blue:255.0/255.0 alpha:1].CGColor;
NSLog(#"on profile table view cell border width is %f and radius is %f and color is %#", _initialLabel.layer.cornerRadius, _initialLabel.layer.borderWidth, _initialLabel.layer.borderColor);
}
The above prints out what I would expect (the correct color + radius + width), but it doesn't show in the view. It's the only reference in code to _initialLabel, so it's not getting set somewhere else in code.
Has anyone run into this and know a fix? Any ideas how to troubleshoot this further?
Related
I have a problem with my application.
In it I want to have 12 round buttons for choosing color. I used IUButton to do this.
Then I used auto layout constrains so the the app will look the same on different sizes of iPhone screen. And here I face a problem:
When I select in simulated metrics in IB "inferred" size of the screen, only 4 inch screen has a circle button.Other, bigger iPhone sizes are showing rectangle buttons with rounded corners.
It looks like the button is resized but is still using the smaller cornerRadius(of a 4 inch iPhone), though the width and height have changed.
But when I choose the same size in simulated metrics and in simulator I have a desired result.
And I have no idea what to do with that! Here is a part of code I am using to make circle buttons. I really hope for help! I really hope that its not a duplicate.
In other words if I don't choose the view in the metric size I get rounded corners, but not the circles. If I choose the same size iPhone in simulator everything is fine (as needed). But I need to have circles while the metrics size is set to inferred.
Here is part of the code I am using:
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
self.isCurrencySelect = NO;
[self createCardTypeList];
self.currencyList = [FinanceUtils currencyCodeListWithAppendedName:NO];
self.pickerContainerView.hidden = YES;
[self.cardTypeButton setTitle:CARDTYPENAME forState:UIControlStateNormal];
[self.currencyButton setTitle:CARDCURRENCYNAME forState:UIControlStateNormal];
self.cardBackgroundView.layer.cornerRadius = 10.0f;
self.cardBackgroundView.layer.borderWidth = 2.0f;
self.cardBackgroundView.layer.borderColor = [UIColor blackColor].CGColor;
self.cardBackgroundView.backgroundColor = [UIColor colorFromHexString:#"#502CA7"];
//change placeholder color
[self.cardNameTextField setValue:[UIColor darkGrayColor] forKeyPath:#"_placeholderLabel.textColor"];
[self.incomeTextField setValue:[UIColor darkGrayColor] forKeyPath:#"_placeholderLabel.textColor"];
//set cards value
self.moneySpentValueLabel.text = #"0";
}
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self updateButtonsColor];
}
-(void) updateButtonsColor{
NSArray *buttonsColorArray = [UIColor cardsColorArray];
for(UIButton *btn in self.buttonsArray){
btn.layer.borderWidth = 2.0f;
btn.layer.cornerRadius = btn.frame.size.width/2;
btn.layer.masksToBounds = YES;
btn.backgroundColor = [buttonsColorArray objectAtIndex:btn.tag];
}
}
The corner radius should not be a constant, try changing it to the button height or something like that.
I'm trying to get smooth corners, but without success.
This is how it looks:
I have Subclass of UIImageView where in layoutSubviews I have the logic for putting the rounded corner for uiimageview, and it's clipped to bounds.
But I'm not getting why some images are drawn with these raw edges, and they are OUT of the border layer...
Because it's clipped, I can't make a custom layer with different frame for border, because whole superlayer is clipped.
- (void)layoutSubviews
{
[super layoutSubviews];
[self layoutIfNeeded];
[self.layer setCornerRadius:self.frame.size.width/2];
[self.layer setBorderWidth:BORDERSIZE];
[self.layer setBorderColor:[BORDERCOLOR CGColor]];
[self setClipsToBounds:YES];
}
To make the circular imageview use this following code in viewDidLoad. For clipped to bounds set it to yes.
self.MyImageView.layer.cornerRadius = self.MyImageView.frame.size.width / 2;
self.MyImageView.clipsToBounds = YES;
To apply the border use this code in viewDidLoad:
self.MyImageView.layer.borderWidth = 3.0f;
self.MyImageView.layer.borderColor = [UIColor whiteColor].CGColor;
This will definitely works out.
I added small UIView at beginning of UITableViewCell's contentView (width = 6px, height = cell.frame.size.height) - this is added in storyboard.
In cellForRowAtIndexPath I reach that view via tag = 9 and set its background color (dynamic).
UIView *colorView = (UIView *)[cell viewWithTag:9];
UIColor *color = nil;
if (indexPath.section == 1)
{
// Category section - add colors here
// Get color view
color = [UIColor colorForCategory:[NewsCategory categoryForString:cellTitle]];
} else
{
color = nil;
}
[colorView setBackgroundColor:color];
That works fine but as indexpath.row is bigger (we go down in tableView) that colored view is being offset more and more down (y) where last cell is overlapped by few pixels of above's cell's color view. Dunno why that happening and its not that obvious until you press cell and new colored view is presented...
i need to make the profile photo image circle (similar to whats app)
The cell i use is UITableViewCellStyleDefault
here's my code:
User *user = [DBHelper fetchUserWithUserID:userID];
cell.imageView.image = [Helper imageWithBlob:user.profile.thumbnailPhotoBlob defaultImageFile:FILE_DEFAULT_PHOTO];
cell.imageView.layer.cornerRadius = cell.imageView.frame.size.height /2;
cell.imageView.clipsToBounds = YES;
cell.imageView.layer.masksToBounds = YES;
But the result image view is still a square
EDIT: The image view's frame is {0, 0, 0, 0}, but it did show the image.
EDIT 2:
I tried the answer but the image is not circle. Here's what i did:
float height = [self tableView:tableView heightForRowAtIndexPath:indexPath];
[Helper circlizeImageView:cell.imageView cellHeihgt:height];
+ (void)circlizeImageView:(UIImageView *)imageView cellHeihgt:(float)cellHeight {
imageView.frame = CGRectMake(0, 0, cellHeight, cellHeight);
[Helper circlizeView:imageView];
}
+ (void)circlizeView:(UIView *)view {
view.layer.cornerRadius = view.frame.size.height / 2;
view.clipsToBounds = YES;
view.layer.masksToBounds = YES;
}
The code is all fine.
The only problem is with this line:
cell.imageView.layer.cornerRadius = cell.imageView.frame.size.height/2;
//CGRect rectTest = cell.imageView.frame;
//returns a rect of (0,0,0,0) & hence the cornerRadius being set is 0.
Try:
cell.imageView.layer.cornerRadius = 10; //or some fixed value
In retrospect, what you're trying to do will not get you to make the imageView circular because the default imageView is rectangular (the height is always more than the width) and so... setting a corner radius that is half of the height/width will not work.
It'll be better if you use a custom UITableViewCell and add a square framed imageView.
ANyways... Since the default imageView touches the top of the default UITableViewCell and ends at the bottom, you can get the height of the imageView by using the height of the row (in case you have dynamically sized cells)
So, something as simple as this:
cell.imageView.layer.cornerRadius = cell.frame.size.height/2;
But none of this looks good since the imageView is rectangularish and cells being of dynamic height will make it look like a poor design (the fixed radius looks best imho)
You have two options here:
1- Add the imageView as a subView to you cell.
2- Override layoutSubviews in a custom cell:
- (void)layoutSubviews {
[super layoutSubviews];
self.imageView.frame = CGRectMake(0,0,40,40);
}
I need to implement this left margin for the background of the cell.
Now I tried to do it this way - but the result is not correct - the content of the cell is moving with the margin. How can I solve an issue?
CGRect layerFrame = self.layer.frame;
layerFrame.size.width -= kRightLayerMargin;
layerFrame.origin.x += kLeftLayerMargin;
self.layer.frame = layerFrame;
self.layer.backgroundColor = [[SCUtils cellSelectionColor] CGColor];
self.layer.cornerRadius = 10.0f;
Implement your cell's layoutSubviews method as follows:
- (void)layoutSubviews {
//have the cell layout normally
[super layoutSubviews];
//get the bounding rectangle that defines the position and size of the textLabel
CGRect textLabelFrame = [[self textLabel] frame];
//anchor it to the top-left corner
textLabelFrame.origin = CGPointMake(kLeftLayerMargin, textLabelFrame.origin.y);
//set the textLabel frame (this will move+resize it)
[[self textLabel] setFrame:textLabelFrame];
//reposition the other controls accordingly
}