I'm trying to center a UIImageView width-wise in a UITableViewCell with the following code:
// self.cover_image is a UIImage variable
// cell is a UITableCellView
UIImageView* backcover_imageview = [[[UIImageView alloc] initWithImage:self.cover_image] autorelease];
[backcover_imageview sizeToFit];
//center image
//float xPos = cell.contentView.frame.size.width - (self.cover_image.size.width/2);
//TODO: need better solution
//Eyeballing attempt:
float xPos = self.cover_image.size.width - 25;
CGRect bookcover_frame = backcover_imageview.frame;
bookcover_frame.origin.x = xPos;
backcover_imageview.frame = bookcover_frame;
backcover_imageview.tag = 9000;
backcover_imageview.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
[cell.contentView addSubview:backcover_imageview];
I'm trying to center the UIImageView in a UITableCellView regardless of what orientation the iPad or iPhone device is in. Does anyone know the right way to do this?
I added two more autoresizing masks and got a good result using your code and PengOne's answer:
playImage.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
[cell.contentView addSubview:playImage];
playImage.center = CGPointMake(cell.contentView.bounds.size.width/2,cell.contentView.bounds.size.height/2);
After you add backcover_imageView to the contentView of the cell, use this to center it:
imageView.center = CGPointMake(cell.contentView.bounds.size.width/2,cell.contentView.bounds.size.height/2);
Can you try assigning the center property of cell to that of image view?
Something like this
imageView.center = cell.center;
And then add it to the cell.
You can use center anchors of the cell to keep the image in the center:
image.centerXAnchor.constraint(equalTo: cell.centerXAnchor).isActive = true
image.centerYAnchor.constraint(equalTo: cell.centerYAnchor).isActive = true
Related
I want to restrict the width of textLabel of UITabelViewCell as it contains a image on its right side.
I don't want to use UILabel or subclass UITabelViewCell.
Try using the accessoryView property for the image on the right. This should prevent the label from getting too wide and truncating the text.
[cell setAccessoryView:<your image view>];
cell.textLabel.numberOfLines = 3; // set the numberOfLines
cell.textLabel.lineBreakMode = UILineBreakModeTailTruncation;
OR
CGRect aFrame = cell.textLabel.frame;
aFrame.size.width = 100; // for example
cell.textLabel.frame = aFrame;
Need to display 4 UIPickerView in ViewController.
How can i customise height?
Minimum height we can given is 162.
Can i give 100 height for each UIPickerView?
Is there any framework or example available?
Try this:
UIPickerView *picker = [[UIPickerView alloc] initWithFrame: CGRectZero];
picker.showsSelectionIndicator = YES;
picker.autoresizingMask = UIViewAutoresizingFlexibleWidth;
CGSize ps = [picker sizeThatFits: CGSizeZero];
picker.frame = CGRectMake(0.0, 0.0, ps.width, ps.height + 100);
Try with following code, it may be work in you case:
self.MyPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
For Other tips check This Answer
UIPicker sizing in landscape mode
For UIPickerView there are only three Height : 162,180,216. You can use CGAffineTransformMakeTranslation and CGAffineTransformMakeScale for properly fit to picker.
CGAffineTransform t;
CGAffineTransform s;
CGAffineTransform u;
UIPickerView *pickerview;
t = CGAffineTransformMakeTranslation (0, pickerview.bounds.size.height);
s = CGAffineTransformMakeScale (1.0,0.5);
u = CGAffineTransformMakeTranslation (0 ,pickerview.bounds.size.height);
pickerview.transform = CGAffineTransformConcat (t, CGAffineTransformConcat(s,u));
Here the above code changes the height of picker view.
If you don't mind clipping the ends, you can embed it inside of a smaller view, with clipping enabled.
In the iPhone calendar app if you have 2 tiles overlaying ontop of each other the text from the bottom tile gets cut off and cannot be seen through the top transparent tile. How would I be able to keep the tiles transparent but not have the text from the bottom tiles show through to the top tiles?
Here is my code
APCalendarDayTile *tile = (APCalendarDayTile *)view;
CGFloat startPos = [APCalendarCurrentDayView yAxisForTime:[APCalendarCurrentDayView minutesToTime:tile.appointment.startDate]];
CGFloat endPos = [APCalendarCurrentDayView yAxisForTime:[APCalendarCurrentDayView minutesToTime:tile.appointment.endDate]];
tile.frame = CGRectMake(kLeftSideBuffer, startPos, (self.bounds.size.width - kLeftSideBuffer - kRightSideBuffer) , endPos - startPos);
tile.backgroundColor = [UIColor colorWithHexString:tile.appointment.appointmentColor]; <-- This also sets the alpha that makes it transparent.
tile.layer.borderColor = [UIColor colorWithHexString:tile.appointment.appointmentColor alpha:1.0].CGColor;
tile.layer.borderWidth = 1.0f;
Tile code
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
CALayer *layer = [self layer];
[layer setMasksToBounds:YES];
[layer setCornerRadius:kCornerRadius];
}
return self;
}
- (id)init
{
if (self = [super init]) {
self.clipsToBounds = YES;
self.userInteractionEnabled = YES;
self.multipleTouchEnabled = NO;
tileTitle = [[UILabel alloc] init];
tileTitle.textColor = [UIColor blackColor];
tileTitle.backgroundColor = [UIColor clearColor];
tileTitle.font = [UIFont boldSystemFontOfSize:13.0f];
[tileTitle setAutoresizesSubviews:YES];
[tileTitle setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
tileDescription = [[UILabel alloc] init];
tileDescription.textColor = [UIColor blackColor];
tileDescription.backgroundColor = [UIColor clearColor];
tileDescription.font = [UIFont systemFontOfSize:11.0f];
tileDescription.lineBreakMode = UILineBreakModeTailTruncation;
[tileDescription setAutoresizesSubviews:YES];
[tileDescription setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[self setAutoresizesSubviews:YES];
[self setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[self addSubview:tileTitle];
[self addSubview:tileDescription];
}
return self;
}
- (void)layoutSubviews
{
CGRect myBounds = self.bounds;
CGSize stringSize = [tileTitle.text sizeWithFont:tileTitle.font];
if (myBounds.size.height <= 22.0) {
tileTitle.frame = CGRectMake(3, 0, myBounds.size.width, stringSize.height);
tileDescription.frame = CGRectMake(stringSize.width + 6, -1, myBounds.size.width, 14);
} else {
tileTitle.frame = CGRectMake(3, 0, myBounds.size.width, stringSize.height);
tileDescription.frame = CGRectMake(5, tileTitle.frame.size.height, myBounds.size.width, 14);
}
}
I would like the text in this photo to not show in the front tiles like the second photo.
#user1272965 is right insofar as each tile needs to know what tiles are above it, then you can get the text effect you're looking for by making the tile views a custom subclass and implementing their drawing manually:
Each tile will need access to tiles that are above it (or their frames at least):
NSInteger tileCount = [tilesAboveMine count];
CGRect framesAboveMine[tileCount];
for (int i=0; i<tileCount; i++) {
framesAboveMine[i] = [tilesAboveMine objectAtIndex:i].frame;
}
In drawRect of your tile view, draw the tile, and then draw the text clipped by the views above it:
// draw aspects of the tile not to be clipped, like the background image.
// the setup for clipping:
CGContextClipToRects(context, framesAboveMine, tileCount);
then draw the text, which will be clipped by the frames above
[myCalendarString drawAtPoint:CGPointMake(10,10)];
What about adding a UIView in between the two boxes? This middle UIView would cover up the letters (and have the same background color as the rear event), which solves your problem: no text coming through, but you'll see the color behind the other label!
You might be able to create the frame for this UIView using the frame attribute of the UILabel as well as the frame attribute of the frontmost calendar appointment.
I think the native calendar app squeezes tiles that occur at the very same start time as you do, but it also squeezes tiles whose times overlap, i.e. if the start time of the later tile is between the start and end time of the earlier tile.
But if you need your UI to draw these tiles overlapping, then there's no way around it: the obscured tile needs to know that it's underneath (starting after and overlapping), and it needs to either hide its labels, or reduce their alpha level.
I have a gallery of UIImageViews (that are SQUARE) declared as subviews of UIScrollView:
int size = self.view.frame.size.width/number;
int maxSize = [list count]; //amount of images to show in this view
imageViewArray = [[NSMutableArray alloc] init];
for (int i=0,j=1,k=0; i<maxSize; i++, j++){
UIImageView *imageView = [[UIImageView alloc] initWithImage:[list objectAtIndex:i]];
if (i % number == 0)
{
j = 1;
k++;
}
imageView.frame = CGRectMake((size*(j-1)), (size*(k-1)), size, size);
imageView.contentMode = UIViewContentModeScaleAspectFit;
[imageView setAutoresizingMask: UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth];
[imageScrollView addSubview:imageView];
imageScrollView.contentSize = CGSizeMake(size*number, size*(maxSize/number));
[imageScrollView setAutoresizesSubviews:YES];
In portrait mode everything is ok. However in landscape i want my images to become bigger both in axe X and Y. With pasted code my images get larger frame only in width, so they are wide and short. I want them to keep their ratio, so still to be square. Any idea..?
BTW: I tried to setAutoresizingMask to flexible height,top and bottom, but it makes my UIImages even shorter (i dont know why).
You don't want your margins to be flexible, leave them fixed, you only want the internal height/width to be flexible. So try setting only the following two:
[imageView setAutoresizingMask: UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
You wrote that the images should increase in size in landscape view, so where are you adjusting the UIImageViews frames? (And if they are getting smaller, then you have a bug in that missing piece of code.)
And #gamozzii is correct, you want the autoresizing on the imageviews to be:
imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
I've added some buttons to an UIView (via addSubview) programmatically. However, they appear as overlays (so that I always see the last button only). How do I add new buttons below existing buttons?
Regards
you can offset the button like this
int newX = previousButton.frame.origin.x + previousButton.frame.size.width ;
int newY = previousButton.frame.origin.y ;
and either set the frame for new button when you create it:
[[UIButton alloc] initWithFrame:CGRectMake(newX,newY,100,100)];
or set the frame later
newButton.frame = CGRectMake(newX,newY,100,100);
Set the UIView's frame origin to layout the UIButtons in the locations you wish:
CGRect buttonFrame = button.frame;
buttonFrame.origin = CGPointMake(100.0f, 100.0f);
button.frame = buttonFrame;
view.addSubview(button);
You can either use the insertSubview:atIndex method or insertSubview:belowSubview of your view.
UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0,100,100)];
[myView insertSubview:myButton belowSubview:previousButton];
OR
[myView insertSubview:myButton atIndex:0];
Thanks for your answers guys.
I did the (horizontal) align with this code:
if([myContainer.subviews lastObject] == nil){
NSLog(#"NIL");
[myContainer insertSubview:roundedButton atIndex:0];
}else{
[myContainer insertSubview:roundedButton belowSubview:[tagsContainer.subviews lastObject]];
}
It works technically, but still overlays the buttons. I have to find a way, how to not overlay them...