Center UIview vertically inside UITableViewCell - uitableview

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;
}
}

Related

Set content inset in PDFView

I am using the new PDFKit framework (https://developer.apple.com/documentation/pdfkit).
I would like, like in UIScrollView, to change the contentInset of my PDFView.
Sadly, PDFView doesn't have a property scrollView or contentInset.
Thanks a lot.
PDFView doesn't have a publicly exposed UIScrollView property, but if you iterate the subviews you should find the that first subview is a PDFScrollView (UIScrollView subclass):
UIView *firstSubview = pdfView.subviews.firstObject;
if ([firstSubview isKindOfClass:[UIScrollView class]]) {
UIScrollView *pdfScrollView = (UIScrollView *)firstSubview;
pdfScrollView.contentInset = UIEdgeInsetsMake(0, 20.0f, 0, 20.0f);
}
Otherwise the only other option I can think of is to inset the view itself:
pdfView.frame = CGRectInset(pdfView.frame, 10.0f, 10.0f);

How to get UIBarButtonItem center position

I am doing some custom view controller transitions and I want my transition to start from the center of the button that was pressed.
For UIButton I can do:
myCustomTransition.startingPoint = buttonPressed.center
But if I want to use an UIBarButtonItem, how to actually tell my transition to start from the center of the button, because I don't see any center property on the UIBarButtonItem.
Any suggestions ?
If you just use UIBarButtonItem as navigationItem.leftBarButtonItem or navigationItem.rightBarButtonItem, you can't directly get there geometry property, because UIBarButtonItem doesn't inherit UIView, but you eventually can get geometry property by using tricky method.
This is the hierachy of a simple ViewController that has left and right UIBarButtonItem:
UINavigationButton is your target, get its rect and you can get the center what you need.
- (void)handleItemPressed {
UIView *targetView = nil;
for (UIView *subView in self.navigationController.navigationBar.subviews) {
if ([subView isKindOfClass:NSClassFromString(#"UINavigationButton")]) {
for (UILabel *label in subView.subviews) {
if ([label isKindOfClass:[UILabel class]]) {
if ([label.text isEqualToString:"right"]) {
targetView = subView;
break;
}
}
}
}
}
CGRect rect = [targetView convertRect:targetView.frame toView:self.view];
CGPoint center_you_need = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
}
If you use customeView,just convert customeView's frame to the point.

UIScrollView content size out of bounds in ios7

I have a wired bug in iOS7, my UIScrollView content height is out of screen bounds and I can't scroll down to the bottom.
I'm using AutoLayout.
The view heirarchy is:
- UIView
- UIScrollView
- UIView
- Buttons
In iOS8+ it's working fine.
I have this in viewDidLoad:
if ([self respondsToSelector:#selector(automaticallyAdjustsScrollViewInsets)]) {
self.automaticallyAdjustsScrollViewInsets = NO;
}
And this also:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.scrollView.contentSize = CGSizeMake(320, 1380);
}

UItableview Separator line's height

Can I adjust the height of the UITableview's separator line? I add UIView at the cell to use as separator line and its good, the problem is that when I slide the cell to delete it, the delete button is the problem, its overlapping the separator line, or can I adjust the delete button's height?
The code pasted in by Rashad is pretty old (found here) and doesn't seem to work for iOS 7 or iOS 8.
Here is updated code that works:
-(void)layoutSubviews {
UIView *deleteButtonView = nil;
for (UIView *subview in self.subviews) {
// find the delete view in iOS 8
if ([NSStringFromClass([subview class]) isEqualToString:#"UITableViewCellDeleteConfirmationView"]){
deleteButtonView = subview;
break;
}
// find the delete view in iOS 7
if ([NSStringFromClass([subview class]) isEqualToString:#"UITableViewCellScrollView"]) {
for (UIView *secondSubview in [subview subviews]) {
if ([NSStringFromClass([secondSubview class]) isEqualToString:#"UITableViewCellDeleteConfirmationView"]) {
deleteButtonView = secondSubview;
break;
}
}
}
}
int heightOffset = 5;
CGRect buttonFrame = deleteButtonView.frame;
buttonFrame.origin.y = heightOffset;
buttonFrame.size.height = self.frame.size.height-2*heightOffset;
deleteButtonView.frame = buttonFrame;
}
If you can't resize the delete button, resize your bottom UIView so it can overlap the delete button.
I always draw separator line like a subView on contentView of cell. And disable separatorStyle in tableView. And customise delete button like here: https://stackoverflow.com/a/22396248/887325
In you TableViewCell layoutSubviews method write this:
if ([NSStringFromClass([subview class]) isEqualToString:#"UITableViewCellDeleteConfirmationControl"]) {
UIView *deleteButtonView = (UIView *)[subview.subviews objectAtIndex:0];
CGRect newf = deleteButtonView.frame;
newf.origin.x = 250;
newf.origin.y = 47;
newf.size.width = 30;
newf.size.height = 50;
deleteButtonView.frame = newf;
}
Hope this helps.. :)

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

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);

Resources