How to vertically center align UIButton in UITableViewCell? (Objective C) - ios

I have a custom table cell to which I want to add a custom UIButton on the left side, vertically centered within the cell. I tried to do that with the code shown below, which is in my custom table cell implementation.
I'm trying to achieve the vertical center alignment by taking the height of self.frame and sizing the button such that it would appear 5px from the top and bottom of the cell. When I run this, I see that the button appears 5px from the top, but considerably more from the bottom (20px or so).
What is the proper way to achieve the vertical alignment?
This is my code in the custom table cell implementation:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
[self setSelectionStyle:UITableViewCellSelectionStyleNone];
// Create the audio button on the left side:
self.audioButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.audioButton.frame = CGRectMake(5, 5, self.frame.size.height - 10, self.frame.size.height - 10);
[self.audioButton setImage:[UIImage imageNamed:#"ec-icon-speaker.png"] forState:UIControlStateNormal];
[self.audioButton addTarget:self
action:#selector(playWordAudio)
forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:self.audioButton];
Thanks for any suggestions,
Erik

Override layoutSubview method of UITableViewCell.
- (void)layoutSubviews
{
[super layoutSubviews];
self.audioButton.center = CGPointMake(self.audioButton.center.x, self.contentView.center.y);
}

You just need to set the frame with related to it's center. ie,
CGFloat height = self.frame.size.height - 10;
self.audioButton.frame = CGRectMake(self.contentView.frame.size.height/2-height/2, 5,height , height);

Related

Vertical align items in UIToolbar

I'm adding a UIToolbar to the bottom of my UIViewController.
- (void)viewDidLoad {
[super viewDidLoad];
self.toolbar = [[UIToolbar alloc] init];
[self.view addSubview:self.toolbar];
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
UIEdgeInsets safeInsets = self.view.safeAreaInsets;
self.toolbar.frame = CGRectMake(0, self.view.frame.size.height-(49.0f+safeInsets.bottom), self.view.frame.size.width, (49.0f+safeInsets.bottom));
}
This gives the UIToolbar the correct height. However, what happens on iPhone X is that the items are vertically centered in the toolbar, resulting in a pretty weird look.
Is there any way to move them to the top? Or should I use the safe area of the iPhone X in a different way?

UITableViewCell contentView frame move too much to the right in editing mode

I've a UIImageView, UILabel, UIButton in my custom UITableViewCell.
When I set editing mode to YES, the entire cell frame moves too much to the right.
Scrolling the table up and down seems to fix it.
But why it happens and how can I really fix it?
In your custom UITableViewCell use layoutSubviews and willTransitionToState.
In your .h file add int state;
In your .m cell add
- (void)willTransitionToState:(UITableViewCellStateMask)aState
{
[super willTransitionToState:aState];
state = aState;
}
Also add this:
- (void)layoutSubviews
{
[super layoutSubviews];
self.contentView.frame = CGRectMake(0,
self.contentView.frame.origin.y,
self.contentView.frame.size.width,
self.contentView.frame.size.height);
if (self.editing )
{
switch (state) {
case 2:
// swipe action -> here you play with how much your content will move
self.contentView.frame = CGRectMake(90,
self.contentView.frame.origin.y,
self.contentView.frame.size.width-90,
self.contentView.frame.size.height);
break;
}
} else {
NSLog(#"subview not in edit %i",state);
self.contentView.frame = CGRectMake(0,
self.contentView.frame.origin.y,
self.contentView.frame.size.width,
self.contentView.frame.size.height);
[self setNeedsDisplay];
}
}
This should be enough to customize indent.
This usually happen due to layout of cell is not getting called use custom cell and set frames in layoutSubviews this will fix your problem

Center UIview vertically inside UITableViewCell

How to vertically center UIviews inside UItableViewCell?
I want to center UIButton vertically inside the UITableViewCell
After a lot of Googling , I found the answer in the blog post a link
This will center the UIView horizontally
myAwesomeSubView.center = cell.contentView.center;
This is the right solution
- (void)layoutSubviews
{
[super layoutSubviews];
for (UIView *view in self.contentView.subviews)
{
view.center = self.contentView.center;
}
}

Hiding UITableView separator behind the contentView(cell.imageView)?

in ios 7 i am using cell built in image view, it behind cell image view but i am try a layout subviews in uitableview custom cell like that
- (void)layoutSubviews {
[super layoutSubviews];
self.imageView.frame = CGRectMake(5.0f , 5.0f, 50.0f, 50.0f);
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
self.textLabel.frame = CGRectMake(60.0f, self.textLabel.frame.origin.y, self.textLabel.frame.size.width, self.textLabel.frame.size.height);
}
image view is set correctly but seperator line in imageview side is empty how to seperator line above a imageview or only way to custom imageview
i add the following line in viewdidload
[self.tableviewName setSeparatorInset:UIEdgeInsetsZero];
now seperater line show fully

Subview won't hide correctly in custom UITableViewCell on toggling of UITableView edit

So my app has an editable and sortable UITableView in its left hamburger basement:
To make sure the table cells were skinny enough to show both the delete button and the sorting drag handle on edit, I created a custom UITableViewCell to handle the editing of the table cells:
Everything works fine on edit, but when I tap done, instead of hiding the delete button hangs around:
The code for this, inside of BookmarkTableViewCell.m, is:
- (void)setEditing:(BOOL)editing animated:(BOOL)animate
{
[super setEditing:editing animated:animate];
if (editing) {
for (UIView * view in self.subviews) {
if([[[view class] description] isEqualToString:#"UITableViewCellReorderControl"])
{
UIView *movedReorderControl = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetMaxX(view.frame), CGRectGetMaxY(view.frame))];
[movedReorderControl addSubview:view];
[self addSubview:movedReorderControl];
CGRect newFrame = movedReorderControl.frame;
newFrame.origin.x = -35;
movedReorderControl.frame = newFrame;
}
}
UIImageView *deleteBtn = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 24, 24)];
[deleteBtn setImage:[UIImage imageNamed:#"icon_delete.png"]];
[self addSubview:deleteBtn];
}
}
Let me know if you need anymore details. Any insight into fixing this would be great. Thanks!
It looks like you're adding the deleteBtn, but you aren't removing it. if editing is false you should locate the deleteBtn cell and remove it from it's superview so it goes away.

Resources