Add Border to the bottom of SELECTED segmented control - ios

I am trying to add a border to the selected segmented control. Currently, I have code which sets the border of 3px to the bottom of the whole segmented control:
CALayer *bottomBorder = [CALayer layer];
bottomBorder.borderColor = [UIColor redColor].CGColor;
bottomBorder.borderWidth = 3;
bottomBorder.frame = CGRectMake(0, self.segmentedControl.frame.size.height - bottomBorder.borderWidth, self.segmentedControl.frame.size.width, bottomBorder.borderWidth);
[self.segmentedControl.layer addSublayer:bottomBorder];
However, I want to add this border ONLY to the selected segmented control. How do I do this?

You can use the following code to dynamically add the selection:
// Removing previous selection
[bottomBorder removeFromSuperlayer];
// Creating new layer for selection
bottomBorder = [CALayer layer];
bottomBorder.borderColor = [UIColor redColor].CGColor;
bottomBorder.borderWidth = 3;
// Calculating frame
CGFloat width = self.segmentedControl.frame.size.width/3;
CGFloat x = self.segmentedControl.selectedSegmentIndex * width;
CGFloat y = self.segmentedControl.frame.size.height - bottomBorder.borderWidth;
bottomBorder.frame = CGRectMake(x, y,width, bottomBorder.borderWidth);
// Adding selection to segment
[self.segmentedControl.layer addSublayer:bottomBorder];
You need to remove the previous selection when new choice is selected, for that purpose make the bottomBorder layer as a member variable.

I converted the Objective-C solutions into swift3 it is working for me, My Segment controller width is equal to View width
#IBAction func SegmentAction(_ sender: UISegmentedControl) {
self.segmentBottomBorder?.removeFromSuperlayer()
self.segmentBottomBorder = CALayer()
self.segmentBottomBorder?.borderColor = UIColor.red.cgColor
self.segmentBottomBorder?.borderWidth = 3
let width: CGFloat = sender.frame.size.width/CGFloat(sender.numberOfSegments)
let x = CGFloat(sender.selectedSegmentIndex) * width
let y = sender.frame.size.height - (self.segmentBottomBorder?.borderWidth)!
self.segmentBottomBorder?.frame = CGRect(x: x, y: y, width: width, height: (self.segmentBottomBorder?.borderWidth)!)
sender.layer.addSublayer(self.segmentBottomBorder!)
}

This might not be the proper way, but you can give it a shot.
At the time you load the segment control, try this in viewdidload, so the layer gets added according to the selectedsegment button
if (self.segmentedControl.selectedSegmentIndex==0) {
CALayer *bottomBorder = [CALayer layer];
bottomBorder.borderColor = [UIColor redColor].CGColor;
bottomBorder.borderWidth = 3;
bottomBorder.frame = CGRectMake(0, self.segmentedControl.frame.size.height - bottomBorder.borderWidth, self.segmentedControl.frame.size.width/2, bottomBorder.borderWidth);
[self.segmentedControl.layer addSublayer:bottomBorder];
}else if (self.segmentedControl.selectedSegmentIndex==1){
//change the border layer frame
} else if (self.segmentedControl.selectedSegmentIndex==2){
//change the border layer frame
}
Again in the segmentcontrol action method, you can alter these changes

//Connect action to segmented control.
- (IBAction)segmentedControlAction:(id)sender {
if(segmentedControl.selectedSegmentIndex == 0)
{
NSLog(#"First Segment (Feed) Selected");
//Remove SuperLayer when segment is selected
[_bottomBorder removeFromSuperlayer];
// Creating new layer for selection
_bottomBorder = [CALayer layer];
_bottomBorder.borderColor = [UIColor redColor].CGColor;
_bottomBorder.borderWidth = 3;
// Calculating frame
CGFloat width = self.segmentedControl.frame.size.width/3;
CGFloat x = self.segmentedControl.selectedSegmentIndex * width;
CGFloat y = self.segmentedControl.frame.size.height - _bottomBorder.borderWidth;
_bottomBorder.frame = CGRectMake(x, y,width, _bottomBorder.borderWidth);
// Adding selection to segment
[self.segmentedControl.layer addSublayer:_bottomBorder];
}
else if(segmentedControl.selectedSegmentIndex == 1)
{
NSLog(#"Second Segment (Stations) Selected");
//Remove SuperLayer when segment is selected
[_bottomBorder removeFromSuperlayer];
// Creating new layer for selection
_bottomBorder = [CALayer layer];
_bottomBorder.borderColor = [UIColor redColor].CGColor;
_bottomBorder.borderWidth = 3;
// Calculating frame
CGFloat width = self.segmentedControl.frame.size.width/3;
CGFloat x = self.segmentedControl.frame.size.width/3;
CGFloat y = self.segmentedControl.frame.size.height - _bottomBorder.borderWidth;
_bottomBorder.frame = CGRectMake(x, y,width, _bottomBorder.borderWidth);
// Adding selection to segment
[self.segmentedControl.layer addSublayer:_bottomBorder];
}
else if(segmentedControl.selectedSegmentIndex == 2)
{
NSLog(#"Third Segment (Events) Selected");
//Remove SuperLayer when segment is selected
[_bottomBorder removeFromSuperlayer];
// Creating new layer for selection
_bottomBorder = [CALayer layer];
_bottomBorder.borderColor = [UIColor redColor].CGColor;
_bottomBorder.borderWidth = 3;
// Calculating frame
CGFloat width = self.segmentedControl.frame.size.width/3;
CGFloat x = self.view.bounds.size.width - self.segmentedControl.frame.size.width/3;
CGFloat y = self.segmentedControl.frame.size.height - _bottomBorder.borderWidth;
_bottomBorder.frame = CGRectMake(x, y,width, _bottomBorder.borderWidth);
// Adding selection to segment
[self.segmentedControl.layer addSublayer:_bottomBorder];
}
}

Related

UILabelView cropping when expanded, but renders fully when inspected in view hierarchy

Image 1 shows issue. label has flexible width & height constrains, also we adding fix width & height to expand with. in View hierarchy it shows fully expanded as expected, but issue in real view:
Image 2 shows fully expanded label in View hierarchy:
Lable code
self.textLabel = [[RAVerticallyAlignedLabel alloc] initWithFrame: self.bounds andVerticalTextAlignment: [self verticalTextAlignment]];
self.textLabel.backgroundColor = ClearColour;
self.textLabel.userInteractionEnabled = NO;
self.textLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.textLabel.numberOfLines = 0;
viewToTruncate = self.textLabel;
textWidth = MAX(MIN(screenSize.width - 20, [label.text sizeWithAttributes: #{NSFontAttributeName : label.font}].width + 10);
[UIView animateWithDuration: 0.3 animations: ^
{
self->containerView.frame = CGRectMake(xPos, top , MIN(textWidth, screenSize.width - 2 * RAPadding), newTextviewHeight);
// Container view is added inside scroll view
self->containerView.layer.shadowRadius = kRATruncationUtiltityShadowRadius;
self->containerView.layer.shadowOpacity = kRATruncationUtiltityShadowOpacity;
self->containerView.layer.shadowOffset = kRATruncationUtiltityShadowOffset;
self->containerView.layer.shadowPath = [UIBezierPath bezierPathWithRect: self->containerView.bounds].CGPath;
self->containerView.layer.masksToBounds = NO;
self->viewToExpand.frame = CGRectMake(0, 0, textWidth + componentContentWidth, self->containerView.height);
//viewToExpand is UILable, added inside container view
self->viewToExpand.layer.borderWidth = 1.0;
self->viewToExpand.layer.borderColor = borderColor.CGColor;
self->viewToExpand.layer.backgroundColor = backgroundColor.CGColor;
}];
Found issue fix, it size wasn't issue but "gradientMask" was playing up on expand as below:
CAGradientLayer *gradientMask = [CAGradientLayer layer];
gradientMask.frame = CGRectMake(0, 0, viewToTruncate.width, viewToTruncate.height);
gradientMask.colors = #[(id)WhiteColour.CGColor,(id)WhiteColour.CGColor, (id)ClearColour.CGColor];
gradientMask.locations = #[#0, #(locationOfFade), #1];
viewToTruncate.layer.mask = gradientMask;
removing mask on expand , it was rendering as expected.

Getting white border on UIView when applying border Width and border Color

I am working on collage app and in collage view I need to set border width and corner radius of frame, the view will be a sublayer of another view and the back view will have background colour as brown.I am able to change the border width and corner radius using slider but after doing that I am getting small tiny border white line (like a shadow) around the view.I tried using shadow opacity but it didn't work. below is my code.
- (IBAction)sliderActionToChangeCornerWidthOfView:(id)sender {
UISlider *slider = (UISlider*)sender;
int i=0;
for (CollageView *viewCollage in self.collageView.subviews) {
if ([viewCollage isKindOfClass:[CollageView class]]) {
self.appDelg.borderWidth = slider.value/2;
if ([[array objectAtIndex:i] valueForKey:#"path"]!=nil) {
if (arrayOfLayers.count > 0) {
[arrayOfLayers makeObjectsPerformSelector:#selector(removeFromSuperlayer)];
}
NSMutableArray *arrayToRadius = [[NSMutableArray alloc] init];
arrayToRadius = [arrayToRadius getArrayOfCollages:_collageView.bounds.size numberOfCollages:numberOfCollages];
NSArray *arrayToChangeCornerRadius=[[[arrayToRadius objectAtIndex:btnClickIndex] valueForKey:#"values"] objectAtIndex:0];
UIBezierPath *starPath=[[arrayToChangeCornerRadius objectAtIndex:i] valueForKey:#"path"];
CAShapeLayer *cornerMaskLayer = [CAShapeLayer layer];
[cornerMaskLayer setPath:starPath.CGPath];
viewCollage.layer.mask = cornerMaskLayer;
CAShapeLayer *strokeLayer = [CAShapeLayer layer];
strokeLayer.path = starPath.CGPath;
strokeLayer.lineWidth = self.appDelg.borderWidth;
strokeLayer.lineJoin = kCALineJoinRound;
// the stroke splits the width evenly inside and outside,
strokeLayer.fillColor = [UIColor clearColor].CGColor;
if (patternImage!=nil) {
strokeLayer.strokeColor = [UIColor colorWithPatternImage:[self patterDraw]].CGColor;
}
else
{
strokeLayer.strokeColor = borderColour.CGColor;
}
CGRect rect=CGPathGetBoundingBox(starPath.CGPath);
[viewCollage layoutIfNeeded];
strokeLayer.shadowOpacity = 0.0;
[arrayOfLayers addObject:strokeLayer];
[viewCollage.layer addSublayer:strokeLayer];
NSLog(#"value : %#", NSStringFromCGRect(viewCollage.frame) );
}
else
{
if (patternImage!=nil) {
viewCollage.layer.borderColor = [UIColor colorWithPatternImage:[self patterDraw]].CGColor;
}
else
{
viewCollage.layer.borderColor = borderColour.CGColor;
}
viewCollage.layer.borderWidth = slider.value/2;
}
i++;
}
}
}
- (IBAction)sliderActionToChangeCornerRadius:(id)sender {
UISlider *slider = (UISlider*)sender;
int i = 0;
for (CollageView *viewCollage in self.collageView.subviews) {
if ([viewCollage isKindOfClass:[CollageView class]]) {
self.appDelg.cornerRadiusValue = slider.value;
if ([[array objectAtIndex:i] valueForKey:#"path"]!=nil) {
if (arrayOfLayers.count > 0) {
[arrayOfLayers makeObjectsPerformSelector:#selector(removeFromSuperlayer)];
}
NSMutableArray *arrayToRadius = [[NSMutableArray alloc] init];
arrayToRadius = [arrayToRadius getArrayOfCollages:_collageView.bounds.size numberOfCollages:numberOfCollages];
NSArray *arrayToChangeCornerRadius=[[[arrayToRadius objectAtIndex:btnClickIndex] valueForKey:#"values"] objectAtIndex:0];
UIBezierPath *starPath=[[arrayToChangeCornerRadius objectAtIndex:i] valueForKey:#"path"];
CAShapeLayer *cornerMaskLayer = [CAShapeLayer layer];
[cornerMaskLayer setPath:starPath.CGPath];
viewCollage.layer.mask = cornerMaskLayer;
CAShapeLayer *strokeLayer = [CAShapeLayer layer];
strokeLayer.path = starPath.CGPath;
strokeLayer.lineWidth = self.appDelg.borderWidth;
strokeLayer.lineJoin = kCALineJoinRound;
// the stroke splits the width evenly inside and outside,
strokeLayer.fillColor = [UIColor clearColor].CGColor;
if (patternImage!=nil) {
strokeLayer.strokeColor = [UIColor colorWithPatternImage:[self patterDraw]].CGColor;
}
else
{
strokeLayer.strokeColor = borderColour.CGColor;
}
CGRect rect=CGPathGetBoundingBox(starPath.CGPath);
[viewCollage layoutIfNeeded];
[arrayOfLayers addObject:strokeLayer];
[viewCollage.layer addSublayer:strokeLayer];
NSLog(#"value : %#", NSStringFromCGRect(viewCollage.frame) );
}
else
{
viewCollage.layer.cornerRadius = slider.value;
}
i++;
}
}
}
Below is the screenshot of the issue which I am facing
Can anyone please help me on this issue.
You can get angle of view by following code:-
CGFloat radians = atan2f(view.transform.b, view.transform.a);
CGFloat degrees = radians * (180 / M_PI);
Can you try once by adding following line:-
UIImageView.clipsToBounds to YES
It will help to get radians and degrees of rotation of particular view.
Hope it works!

Modify frame of the frontView in SWRevealViewController

Typically, if one is using SWRevealViewController, the front view fully covers the rear view, and the rear view is visible only if some action is performed (swipe, tap etc). My requirement is that the rear view should always be visible at least a little bit when the view loads and when the user swipes back. I tried modifying the frame of the frontView to achieve this, but I saw no effect whatsoever.
I put the frame update frame code in a method:
-(void)updateFrontViewControllerFrameWithPadding:(BOOL)bProvidePadding
{
if (!bProvidePadding) {
CGRect frontViewControllerFrame = self.frontViewController.view.bounds;
CGFloat xOrigin = self.view.bounds.origin.x;
CGFloat nWidth = self.view.bounds.size.width;
frontViewControllerFrame.origin.x = xOrigin;
frontViewControllerFrame.size.width = nWidth;
self.frontViewController.view.frame = frontViewControllerFrame;
_frontViewShadowColor = [UIColor blackColor];
[_contentView reloadShadow];
}
else {
CGFloat nPadding = 0.0f;
if ([IILUtility targetDevice] == UIUserInterfaceIdiomPad) {
_rearViewRevealWidthIpad = 20.0f;
nPadding = _rearViewRevealWidthIpad;
}
else {
_rearViewRevealWidthIphone = 15.0f;
nPadding = _rearViewRevealWidthIphone;
}
CGRect revealViewBounds = self.view.bounds;
CGFloat frontViewXPos = revealViewBounds.origin.x + nPadding;
CGFloat frontViewWidth = revealViewBounds.size.width - nPadding;
CGRect frontViewFrame = self.frontViewController.view.frame;
frontViewFrame.origin.x = frontViewXPos;
frontViewFrame.size.width = frontViewWidth;
self.frontViewController.view.frame = frontViewFrame;
_frontViewShadowColor = [UIColor colorWithWhite:0.5
alpha:0.5];
[_contentView reloadShadow];
//reset rearViewRevealWidth
[self resetRearViewRevealWidth];
}
}
This method is called from the viewWillLayoutSubviews method, with providePadding argument as TRUE. This method also needs to be called from places in SWRevealViewController where pan and tap gestures have been handled.
This is how the view looks when swiped:
This is the solution to the frame resizing problem:
-(void)updateFrontViewControllerFrameWithPadding:(BOOL)bProvidePadding
{
if (!bProvidePadding) {
CGRect frontViewControllerFrame = self.frontViewController.view.bounds;
CGFloat xOrigin = self.view.bounds.origin.x;
CGFloat nWidth = self.view.bounds.size.width;
frontViewControllerFrame.origin.x = xOrigin;
frontViewControllerFrame.size.width = nWidth;
self.frontViewController.view.frame = frontViewControllerFrame;
_frontViewShadowColor = [UIColor blackColor];
[_contentView reloadShadow];
//There is a catch here. The shadow for _frontView is different from that of self.frontViewController.view
//If we do not modify the shadow for self.frontViewController.view then it overlaps with
// the shadow of _frontView. This is generally not a problem in the case when the rear view is in focus and the
//front view is toggled to the right. But if we need the front view to be shifted slighlty to the right even when
//its position is FrontViewPositionLeft, it is important to manage the shadow of self.frontViewController.view
//otherwise the shadow takes up width equal to the padding we have provided.
CALayer *frontViewLayer = self.frontViewController.view.layer;
frontViewLayer.shadowColor = [UIColor colorWithWhite:0.8f
alpha:0.0f].CGColor;
frontViewLayer.shadowOpacity = 0.0f;
frontViewLayer.shadowOffset = CGSizeMake(0.0f, 0.0f);
frontViewLayer.shadowRadius = 0.0f;
}
else {
CGFloat nPadding = 0.0f;
if ([IILUtility targetDevice] == UIUserInterfaceIdiomPad) {
_rearViewRevealWidthIpad = 60.0f;
nPadding = _rearViewRevealWidthIpad;
}
else {
_rearViewRevealWidthIphone = 35.0f;
nPadding = _rearViewRevealWidthIphone;
}
CGRect revealViewBounds = self.view.bounds;
CGFloat frontViewXPos = revealViewBounds.origin.x + nPadding;
CGFloat frontViewWidth = revealViewBounds.size.width - nPadding;
CGRect frontViewFrame = self.frontViewController.view.frame;
frontViewFrame.origin.x = frontViewXPos;
frontViewFrame.size.width = frontViewWidth;
self.frontViewController.view.frame = frontViewFrame;
[self rearViewDeploymentForLeftPosition];
_frontViewShadowColor = [UIColor colorWithWhite:0.8f
alpha:0.0f];
[_contentView reloadShadow];
//There is a catch here. The shadow for _frontView is different from that of self.frontViewController.view
//If we do not modify the shadow for self.frontViewController.view then it overlaps with
// the shadow of _frontView. This is generally not a problem in the case when the rear view is in focus and the
//front view is toggled to the right. But if we need the front view to be shifted slighlty to the right even when
//its position is FrontViewPositionLeft, it is important to manage the shadow of self.frontViewController.view
//otherwise the shadow takes up width equal to the padding we have provided.
CALayer *frontViewLayer = self.frontViewController.view.layer;
frontViewLayer.shadowColor = [UIColor blackColor].CGColor;
frontViewLayer.shadowOpacity = _frontViewShadowOpacity;
frontViewLayer.shadowOffset = _frontViewShadowOffset;
frontViewLayer.shadowRadius = _frontViewShadowRadius;
//reset rearViewRevealWidth
[self resetRearViewRevealWidth];
}
}
This also takes into account the shadow related problem mentioned above.
This method needs to be called from appropriate places, eg; when the view loads initially, when pan or tap gestures are performed etc.

iOS - Shadow won't resize correctly

I have a tableView with dynamic cell sizes, and having trouble resizing the shadow when the cells are being reused. This solution works on iOS8 but not on iOS7. Also the shadows show up correctly based on the cell size the first time the shadow is create, but they break after cells are being reused.
#implementation
static const CGFloat shadowWithInset = 2;
static const CGFloat shadowHeightOutset = 4;
- (void)awakeFromNib {
CALayer *layer = self.innerView.layer;
layer.shadowOffset = CGSizeMake(1, 1);
layer.shadowRadius = 3.0f;
layer.shadowColor = [[UIColor gray] CGColor];
layer.shadowOpacity = 0.8f;
//layer.shouldRasterize = YES;
//layer.rasterizationScale = [UIScreen mainScreen].scale;
CGSize size = layer.bounds.size;
layer.shadowPath = [UIBezierPath bezierPathWithRect:CGRectMake(shadowWithInset, size.height-shadowHeightOutset, size.width-(shadowWithInset*2), shadowHeightOutset)].CGPath;
}
- (void)layoutSubViews {
[super layoutSubviews];
CGSize size = self.innerView.bounds.size;
layer.shadowPath = [UIBezierPath bezierPathWithRect:CGRectMake(shadowWithInset, size.height-shadowHeightOutset, size.width-(shadowWithInset*2), shadowHeightOutset)].CGPath;
}
#end
After populating all textfields in cellForRowAtIndexPath I ended up doing this:
[self setNeedsLayout];
[self layoutIfNeeded];

Animating custom view half way the screen with an image in it - abrupt animation

In one of my views I have a bottom bar. A tap on this view animates a temporary cart half way to the screen. Now, where there is no item in the temporary table I show a no data overlay which is nothing but a view with an empty cart image and text underneath it.
The issue is: when this table animates to expand and collapse, the no data overlay does not look good during animation. It appears that the image is also shrinking/expanding until the temporary table view stops animating.
So, I tried by playing with the alpha of the temporary table view while this animation happens so that it does not look that bad. But this is not helping either. There is an abrupt flash in the end.
Any suggestions?
self.temporaryTable.backgroundView = self.noDataOverlay;
[UIView animateWithDuration:0.5 animations:^{
self.temporaryTable.alpha = 0.5;
} completion:^(BOOL finished) {
self.temporaryTable.alpha = 1.0;
}];
Here is my code for drawing the image:
- (void)drawRect:(CGRect)iRect scalingImage:(BOOL)iShouldScale {
CGRect aRect = self.superview.bounds;
// Draw our image
CGRect anImageRect = iRect;
if (self.image) {
//scale the image based on the height
anImageRect = CGRectZero;
anImageRect.size.height = self.image.size.height;
anImageRect.size.width = self.image.size.width;
#ifdef ACINTERNAL
if (iShouldScale) {
anImageRect = [self aspectFittedRect:anImageRect max:aRect];
} else {
anImageRect.origin.x = (iRect.size.width/2) - (anImageRect.size.width/2);
anImageRect.origin.y = kTopMargin;
}
#else
anImageRect.origin.x = (iRect.size.width/2) - (anImageRect.size.width/2);
anImageRect.origin.y = kTopMargin;
#endif
if (self.shouldCenterImage)
anImageRect.origin.y = (iRect.size.height/2) - (anImageRect.size.height/2);
[self.image drawInRect:anImageRect];
} else {
anImageRect.origin.y = (iRect.size.height/6);
anImageRect.size.height = (iRect.size.height/6);
}
// Draw our title and message
if (self.title) {
CGFloat aTop = anImageRect.origin.y + anImageRect.size.height + kSpacer;
CGFloat aWidth = aRect.size.width;
UIColor *aColor = [UIColor colorWithRed:96/256.0 green:106/256.0 blue:122/256.0 alpha:1];
[aColor set];
CGRect aTextRect = CGRectMake(0, aTop, aWidth, kTitleHeight * 2);
UIFont *aTitleFont = [UIFont boldSystemFontOfSize:kTitleFontSize];
[self.title drawInRect:aTextRect withFont:aTitleFont lineBreakMode:NSLineBreakByClipping alignment:NSTextAlignmentCenter];
if (self.subTitle) {
UIFont *aSubTitleFont = [UIFont systemFontOfSize:kSubTitleFontSize];
aTextRect = CGRectMake(0, aTop+kSpacer, aWidth, kTitleHeight);
[self.subTitle drawInRect:aTextRect withFont:aSubTitleFont lineBreakMode:NSLineBreakByClipping alignment:NSTextAlignmentCenter];
}
}
}
- (void)addToView:(UIView *)iView {
// setting a unique tag number here so that if the user has any other tag there should not be a conflict
UIView *aView = [iView viewWithTag:699];
if (aView) {
[aView removeFromSuperview];
}
self.tag = 699;
[iView addSubview:self];
}
- (void)removeView {
[super removeFromSuperview];
}
-(void)setViewFrame:(CGRect) iFrame {
CGRect aRect = self.frame;
aRect.size.width = iFrame.size.width;
aRect.size.height = iFrame.size.height;
self.frame = aRect;
[self setNeedsDisplay];
}
- (CGRect)aspectFittedRect:(CGRect)iRect max:(CGRect)iMaxRect {
float anOriginalAspectRatio = iRect.size.width / iRect.size.height;
float aMaxAspectRatio = iMaxRect.size.width / iMaxRect.size.height;
CGRect aNewRect = iMaxRect;
if (anOriginalAspectRatio > aMaxAspectRatio) { // scale by width
aNewRect.size.height = iMaxRect.size.width * iRect.size.height / iRect.size.width;
} else {
aNewRect.size.width = iMaxRect.size.height * iRect.size.width / iRect.size.height;
}
aNewRect.origin.y = (iMaxRect.size.height - aNewRect.size.height)/2.0;
aNewRect.origin.x = (iMaxRect.size.width - aNewRect.size.width)/2.0;
return CGRectIntegral(aNewRect);
}
One possibility here is to fix the original problem, namely the fact that the empty cart image is expanding/collapsing as the view animates. Without seeing your code it is difficult to solve this problem, but in my own code what I do is set the contentMode of the UIImageView to UIViewContentModeBottom. This causes the image to stay the same size even though the image view that contains it may grow and shrink as part of the animation.
You see a flash because you animate alpha up to 0.5 and then when animation completes you set it from 0.5 to 1.0. Just animate the alpha up to 1.0:
[UIView animateWithDuration:0.5
animations:^
{
self.temporaryTable.alpha = 1.0;
}];

Resources