Rounded views not working in iOS 9 - ios

I have a code that takes a view and rounds it corners - turning it into a circle:
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 8, 8)];
imageView.layer.cornerRadius = imageView.frame.size.width/2;
imageView.layer.masksToBounds = YES;
This works great on iOS 7 and iOS 8 - but doesn't work on iOS 9.
How can I fix it?

Turns out adding a background color to the ImageView fixes the issue...

Thanks to #YogevSitton answer I've searched over docs and found this:
It appears to be something in apple docs:
By default, the corner radius does not apply to the image in the
layer’s contents property; it applies only to the background color and
border of the layer.
So, to set cornerRadius to UIImageView it needs to have backgroundColor.

If this is not working for anymore, make a component. Subclass from UIView. Do this changes on this subclass:
subclass.layer.cornerRadius = subclass.frame.size.width/2;
subclass.layer.masksToBounds = YES;
and add the imageView to this view. This will solve the problem.
#implementation CircleImageView{
UIImageView* imageView;
CGFloat width;
}
- (instancetype) initWithTopLeft:(CGPoint)topLeft width:(CGFloat)wdth{
self = [super initWithFrame:CGRectMake(topLeft.x, topLeft.y, wdth, wdth)];
if (self) {
width = wdth;
[self createContent];
self.layer.cornerRadius = width/2.;
self.clipsToBounds = YES;
}
return self;
}
- (void) createContent{
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width, width)];
[self addSubview:imageView];
}

Try adding Quartzcore.framework in Linked Framework and Libraries
then importing the same in your viewController.m file

I jsut subClass UIImageView ,like this ,I use PureLayout control the imageView,like below:
[imageViewIcon autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.contentView withOffset:20];
[imageViewIcon autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self.contentView withOffset:10];
[imageViewIcon autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:self.contentView withOffset:-10];
[imageViewIcon autoMatchDimension:ALDimensionHeight toDimension:ALDimensionWidth ofView:imageViewIcon];
-(instancetype)init
{
if (self = [super init]) {
}
return self;
}
-(void)layoutSubviews
{
[super layoutSubviews];
[self layoutIfNeeded];
self.layer.masksToBounds = YES;
self.layer.cornerRadius = self.width*0.5;
}

Related

Add a 0.5 point height subview to UINavigationBar not show in iOS7

I have a class inherit from UINavigationBar and I want to add a separator line as its subview to separate the navigation bar and the navigation content.
The line's height is defined as following.
#define SEPERATOR_LINE_HEIGHT (1.0f / [UIScreen mainScreen].scale)
My code:
#interface MyPopNavigationBar : UINavigationBar
#end
#implementation MyPopNavigationBar {
UIView *separatorLine;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.clipsToBounds = YES;
self.translucent = NO;
separatorLine = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.bounds) - SEPERATOR_LINE_HEIGHT, CGRectGetWidth(self.bounds), SEPERATOR_LINE_HEIGHT)];
separatorLine.backgroundColor = [UIColor redColor];
separatorLine.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
[self addSubview:separatorLine];
}
return self;
}
This works well in both iOS 6 and iOS 8 (all Retina), but I can't see my separatorLine in iOS 7 (Retina, too)!
iOS 6 & 8:
iOS 7:
Besides, when I tried to set the separator line height to exact 1, it shows in all iOS versions.
separatorLine = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.bounds) - 1, CGRectGetWidth(self.bounds), 1)];
What's wrong?
Solved this by myself. It's the autoresizingMask's fault 😂. I strongly suspect this a bug of iOS7.
I print the recursiveDescription in lldb, only to find the height of separatorLine is autoresized to zero in iOS7! In contrast, the value is 0.5 in iOS8.
So, remove this line:
separatorLine.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
And set the frame in layoutSubviews method of MyPopNavigationBar again to make it correct:
- (void)layoutSubviews {
[super layoutSubviews];
separatorLine.frame = CGRectMake(0, CGRectGetHeight(self.bounds) - SEPERATOR_LINE_HEIGHT, CGRectGetWidth(self.bounds), SEPERATOR_LINE_HEIGHT);
}
Then the line displays in all iOS versions.

initWithImage works fine initWithFrame is being called more than once

I am sublassing either an UIImageView or UIView for generating simple color tiles with digits. If I am using UIImageView, I use initWithImage method. If I use UIView, the method initWithFrame is being used.
Either red square image or programmatically generated red view is used for initialization.
The problem can be seen on two screenshots: when initWithImage is being used - everything works fine. If initWithFrame method is being used, I am ending with multiple white views without any information created in even order with normal views. All the screenshots and code attached.
This how it looks when initializing using initWithImage:
- (id)initWithImage:(UIImage *)image {
self = [super initWithImage:image];
if (self) {
//Some non-important, label-related stuff.
[self addSubview:self.numberLabel];
}
return self;
}
And this is how it looks with initWithFrame:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.redView = [self redViewWithFrame:frame];
[self addSubview:self.redView];
//label-related stuff
}
return self;
}
- (UIView *)redViewWithFrame:(CGRect)frame {
UIView *view = [[UIView alloc] initWithFrame:frame];
view.backgroundColor = [UIColor redColor];
view.alpha = 1;
return view;
}
And the for-loop, calling these initializers from another class (UIScrollView subclass). The label value is being set after the view had been initialized.
- (void)setNumberOfBlocks:(NSInteger)numberOfBlocks {
_blocks = [[NSMutableArray alloc] init];
CGSize contentSize;
contentSize.width = BLOCK_WIDTH * (numberOfBlocks);
contentSize.height = BLOCK_HEIGHT;
self.contentSize = contentSize;
for (int i = 0; i < numberOfBlocks; i++) {
CGFloat totalWidth = BLOCK_WIDTH * i;
CGRect frame = CGRectMake(0, 0, BLOCK_WIDTH, BLOCK_HEIGHT);
frame.origin.x = totalWidth;
BlockView *view = [[BlockView alloc] initWithImage:[UIImage imageNamed:#"block.png"]];
OR!!!
BlockView *view = [[BlockView alloc] initWithFrame:frame];
view.frame = frame;
NSString *number = [NSString stringWithFormat:#"%ld", (long)i + 1];
view.numberLabel.text = number;
[self addSubview:view];
[_blocks addObject:view];
}
}
Googling gave me the impression that this is very common problem, but I haven't found any solution how to beat this. Moreover, I still do not understand, why numbers are in totally right order, the only problem is view position.
The problem is that you're setting the frame of the red view to the superview's frame instead of its bounds. Since the red view is a subview of the BlockView, its frame needs to be relative to its superview, which you get with bounds (its not clear why you even need the red view, as opposed to setting the background color of the block view to red).
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.redView = [self redViewWithFrame:self.bounds];
[self addSubview:self.redView];
//label-related stuff
}
return self;
}
I think you should set the frame origin to (0,0) in redViewWithFrame in ordre to hâve superposed views

The background colour of a grouped UITableView will not go transparent in iOS7

I am working on an app, which worked perfectly fine with iOS 6. With iOS7 the app has couple of layout and general few appearance issues. One is not being able to set the background color of a grouped tableview to transparent. Heres is my code which does not work anymore
tableForDetails = [[UITableView alloc]initWithFrame:CGRectMake(0, yAxisTable, 320, 150) style:UITableViewStyleGrouped];
UIColor *backGroundColor = [UIColor clearColor];
UIView *bview = [[UIView alloc]init];
[bview setBackgroundColor:backGroundColor];
[tableForDetails setBackgroundView:bview];
Help is greatly appreciated. Thanks
To set background transparency for a grouped uiTableview in iOS7 is really quite easy. The solution is even more simple than I originally thought.
[tableForDetails setBackgroundColor:[UIColor clearColor]];
or for only semi transparent
[tableForDetails setBackgroundColor:[[UIColor alloc]initWithRed:1.0 green:1.0 blue:1.0 alpha:0.5]];
This worked fine for me.
I fixed it with this:
tableView.backgroundView = UIView.new;
I put any kind of improvements on Apple components in a super class. The following upgrades are applied in BasicTableView.m, which extends UITableView:
1) overrides setBackgroundColor to work with iOS 7 and above, while maintaining backward compatibility with iOS 6 and 5.
2) overrides initWithCoder: and initWithFrame:style: to initialize the background color to a default transparent background
- (id) initWithCoder:(NSCoder *)decoder
{
self = [super initWithCoder:decoder];
if (self) {
[self setup];
}
return self;
}
- (id) initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
self = [super initWithFrame:frame style:style];
if (self) {
[self setup];
}
return self;
}
- (void) setup
{
//Set the BG color to clear color by default
[self setBackgroundColor:[UIColor clearColor]];
}
- (void) setBackgroundColor:(UIColor *)backgroundColor
{
if (self.style == UITableViewStyleGrouped) {
if (majorSystemVersion() < 7) {
UIView *tableBgView = [[UIView alloc] init];
tableBgView.backgroundColor = backgroundColor;
self.backgroundView = tableBgView;
} else {
[super setBackgroundColor:backgroundColor];
}
}
}
//majorSystemVersion() is defined elsewhere in a utility file
int majorSystemVersion(void) {
NSString *systemVersion = [UIDevice currentDevice].systemVersion;
NSArray *systemVersionComps = [systemVersion componentsSeparatedByString:#"."];
NSString *majorVersion = [systemVersionComps objectAtIndex:0];
return [majorVersion intValue];
}
UITableViewCell is in white Background by default in iOS 7
put this inside your cellforRowAtIndexPath
[cell setBackgroundColor:[UIColor clearColor]]

Is this the way to create a custom UIView?

I've subclassed UIView so I can add multiple view of the same type to my project. The view is basically a rectangle with a radius, a picture in the left side and a piece of text on the right. Now don't get me wrong the code bellow works and all but i'm always asking myself "is this the way, or am I getting it really wrong".
1) I'm mixing CALayers with UILabels, is this ok?
2) Is the setUpView the way its done.
3) If this is ok, whats my best bet for letting the imageLayer respond to touch events?
4) Could I just ask a side question as well. As a part time beginner with only one basic app in the store am I been a bit picky. What I mean is, should I just make it work and crack on! What do you think?
#synthesize dateLabel = _dateLabel;
#synthesize nameLabel = _nameLabel;
#synthesize photo = _photo;
#synthesize roundRect= _roundRect;
#synthesize imageLayer = _imageLayer;
-(void)setUpView
{
//create the white rectangle
self.roundRect.frame = CGRectMake(0.0, 0.0, self.frame.size.width, self.frame.size.height);
self.roundRect.cornerRadius = 15.0;
self.roundRect.backgroundColor = [UIColor clearColor].CGColor;
self.roundRect.borderColor = [UIColor whiteColor].CGColor;
self.roundRect.borderWidth = 2.0;
self.roundRect.masksToBounds = YES;
//create the photo
CGImageRef image = [self.photo CGImage];
self.imageLayer.frame = CGRectMake(0.0, 0.0, self.roundRect.frame.size.width*0.48, self.roundRect.frame.size.height);
self.imageLayer.borderColor = [UIColor whiteColor].CGColor;
self.imageLayer.borderWidth = 2.0;
self.imageLayer.masksToBounds = YES;
[self.imageLayer setContents:(__bridge id)image];
[self.imageLayer setContentsGravity:kCAGravityResizeAspectFill];
[self.imageLayer setContentsRect:CGRectMake(0.0,0.0,1.1,1.0)];
//create label
self.nameLabel.frame = CGRectMake(self.imageLayer.frame.size.width+5, self.roundRect.frame.size.height*0.05, self.roundRect.frame.size.width*0.48,self.roundRect.frame.size.height*0.35);
self.nameLabel.backgroundColor = [UIColor clearColor];
self.nameLabel.textAlignment = UITextAlignmentCenter;
self.nameLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
self.nameLabel.textColor = [UIColor whiteColor];
self.nameLabel.adjustsFontSizeToFitWidth =YES;
self.nameLabel.font = [UIFont fontWithName:#"Gill Sans" size:50.0];
[self.roundRect addSublayer:self.imageLayer];
[self.layer addSublayer:self.roundRect];
[self addSubview:self.nameLabel];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
}
-(void)setPhoto:(UIImage *)photo
{
_photo = photo;
[self setUpView];
}
-(void)setNameLabel:(UILabel *)nameLabel
{
_nameLabel = nameLabel;
[self setUpView];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.roundRect = [[CALayer alloc]init];
self.imageLayer = [[CALayer alloc]init];
self.nameLabel = [[UILabel alloc]init];
[self setUpView];
}
return self;
}
You could make this easier by using the Interface Builder. I would create a XIB with a UIView set to the size I want. Then add a label (nameLabel) and configure it how you like. You can use QuartzCore layers cornerRadius to set your rounded corners. Then you can use your subclass in the XIB and set it as the class type for the UIView. Not the file owner but the view. Then you can link your label as an outlet and you won't need to create it manually. You can remove some of the code above and still have your functionality and looks. I've learned to let the Interface Builder do the work when you can.
Hope this helps.

Custom UIPopoverBackgroundView: no drop shadow

I've used this good tutorial to create a custom UIPopoverBackgroundView class.
It works well. The only problem is that I am not getting the typical UIPopoverController drop shadow and I want it. I've tried specifying it on the layer of my UIPopoverBackgroundView instance without success. My instance of UIPopoverController doesn't seem to have a public view to manipulate. Adding it to the popover content also doesn't work.
Probably really simple: how do I add a drop shadow when using a custom UIPopoverBackgroundView class?
// UIPopoverBackgroundView.m
-(id)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
_borderImageView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:#"bg-popover.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(CAP_INSET,CAP_INSET,CAP_INSET,CAP_INSET)]];
_arrowView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"bg-popover-arrow.png"]];
[self addSubview:_borderImageView];
[self addSubview:_arrowView];
self.layer.shadowOffset = CGSizeMake(50, 50);
self.layer.shadowColor = [[UIColor blackColor] CGColor];
}
return self;
}
You don't need to add your own shadows. The base UIPopoverBackgroundView will do it for you. Just make sure to call super in your layoutSubviews implementation.
EDIT: My comment applies to apps targeting iOS 6.
Ok, figured it out. I needed to add the drop shadow to the borderImageView, not the view of the popover instance.
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
_borderImageView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:#"bg-popover.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(CAP_INSET,CAP_INSET,CAP_INSET,CAP_INSET)]];
_arrowView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"bg-popover-arrow.png"]];
[self addSubview:_borderImageView];
[self addSubview:_arrowView];
_borderImageView.layer.cornerRadius = 5.0f;
_borderImageView.layer.masksToBounds = NO;
_borderImageView.layer.borderWidth = 1.0f;
_borderImageView.layer.borderColor = [UIColor blackColor].CGColor;
_borderImageView.layer.shadowColor = [UIColor blackColor].CGColor;
_borderImageView.layer.shadowOpacity = 0.8;
_borderImageView.layer.shadowRadius = 50;
_borderImageView.layer.shadowOffset = CGSizeMake(-10.0f, 10.0f);
}
return self;
}

Resources