ios: UIButton title format delay - ios

I've got some buttons where I left align and line wrap the title text.
When I run the app, there is a delay from where the text is unformatted:
pre-format text http://imageshack.us/a/img28/1958/preformat.jpg
To when the alignment and wrapping take effect:
post-format text http://imageshack.us/a/img821/6118/postformat.jpg
On the simulator the delay is quite noticeable (~ 1 second), on my mate's iPhone 5 the delay is much smaller but still noticeable.
What causes this delay? Can I get around this delay?
Thanks in advance!
EDIT
In my viewDidLoad method I create 10 buttons:
for (int i = 1; i <= STEP_NUMBER; ++i)
[self createStepButtion:i isIphone:isIphone isRetina:isRetina];
In createStepButton, the relevant code is below:
UIButton * b = [UIButton buttonWithType:UIButtonTypeCustom];
// Listener
[b addTarget:self action:#selector(goToStep:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:b];
// Set button size.
CGFloat width = self.scrollView.frame.size.width;
// CGRectMake(x, y, width, height)
int y = stepNum * margin + (stepNum - 1) * buttonHeight;
int buttonWidth = width - margin * 2;
b.frame = CGRectMake(margin, y, buttonWidth, buttonHeight);
b.layer.cornerRadius = cornerRadius;
b.clipsToBounds = YES;
b.showsTouchWhenHighlighted = YES;
// Image
[b setImage:img forState:UIControlStateNormal];
b.imageEdgeInsets = [self makeImageInsets:buttonWidth];
// Text
[b setTitleEdgeInsets:UIEdgeInsetsMake(0, titleLeftMargin, 0, titleRightMargin)];
b.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
b.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
[b setTitle:[NSString stringWithFormat:#"%i - %s", stepNum, STEP_NAMES[stepNum - 1]]forState:UIControlStateNormal];
[b setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
b.titleLabel.font = [UIFont systemFontOfSize:fontSize];
// Background
[b setBackgroundColor:[UIColor whiteColor]];
// The step number this button represents.
b.tag = stepNum;

Related

Bringing UiScrollView element to center when clicked

What I have is UIScrollView and inside it I have buttons of different different sizes.
- (void)viewDidLoad {
[super viewDidLoad];
myScrollView.frame = CGRectMake(0, 100, 375, 50.0);
int totalButtonsToAdd = 20;
startX = 0;
int buttonWidth = 60;
for (int i=0;i<totalButtonsToAdd;i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:[NSString stringWithFormat:#"Btn-%d", i] forState:UIControlStateNormal];
[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
button.accessibilityValue = [NSString stringWithFormat:#"%d", i+1];
button.frame = CGRectMake(startX, 0, buttonWidth, 50.0);
[[button layer] setBorderWidth:2.0f];
[[button layer] setBorderColor:[UIColor greenColor].CGColor];
button.layer.cornerRadius = 5;
startX = startX + buttonWidth + 5;
buttonWidth = buttonWidth + 5;
[myScrollView addSubview:button];
}
NSLog(#"startX===%d", startX);
myScrollView.contentSize = CGSizeMake(startX, 50.0);
}
-(IBAction)aMethod:(id)sender {
UIButton *mButton = (UIButton *)sender;
NSLog(#"clicked button index is %#", mButton.accessibilityValue);
}
What I want to do is when I click any button, I want to bring that button in center. For this I believe I will need to scroll the scrollview to specific position.
For this what I did is as below.
- (void)viewDidLoad {
[super viewDidLoad];
myScrollView.frame = CGRectMake(0, 100, 375, 50.0);
int totalButtonsToAdd = 20;
startX = 0;
int buttonWidth = 60;
for (int i=0;i<totalButtonsToAdd;i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:[NSString stringWithFormat:#"Btn-%d", i] forState:UIControlStateNormal];
[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
button.accessibilityValue = [NSString stringWithFormat:#"%d", i+1];
button.frame = CGRectMake(startX, 0, buttonWidth, 50.0);
[[button layer] setBorderWidth:2.0f];
[[button layer] setBorderColor:[UIColor greenColor].CGColor];
button.layer.cornerRadius = 5;
startX = startX + buttonWidth + 5;
NSLog(#"startX===%d==%d", i, startX);
buttonWidth = buttonWidth + 5;
[myScrollView addSubview:button];
}
NSLog(#"startX final===%d", startX);
myScrollView.contentSize = CGSizeMake(startX, 50.0);
}
-(IBAction)aMethod:(id)sender {
UIButton *mButton = (UIButton *)sender;
NSLog(#"clicked button index is %#", mButton.accessibilityValue);
focusPosition = 0;
int totalButtonsToAdd = 20;
int buttonWidth = 60;
startX = 0;
for (int i=0;i<totalButtonsToAdd;i++) {
startX = startX + buttonWidth + 5;
buttonWidth = buttonWidth + 5;
if (i==[mButton.accessibilityValue intValue]) {
focusPosition = startX;
}
}
NSLog(#"focusPosition===%d", focusPosition);
CGRect frame = CGRectMake(focusPosition, 0, buttonWidth, 50); //wherever you want to scroll
[myScrollView scrollRectToVisible:frame animated:NO];
}
However this don't give me exact what I want. Is there any property for scrollview that will bring that button in center?
Project with above code
Note: This is sample for iPhone 6 only. So please run project on iPhone 6 simulator only
UICollectionView would probably have been a better choice than a UIScrollView, since it adds a method to achieve just what you want (scrollToItemAtIndexPath:atScrollPosition:animated:).
If you're sticking with UIScrollView, you'll need to use setContentOffset:animated: instead, and compute the desired offset, e.g.:
-(IBAction)aMethod:(UIButton *)button {
CGPoint contentOffset = CGPointMake(CGRectGetMidX(button.frame) - (CGRectGetWidth(self.scrollView.frame) / 2.0),0.0);
[myScrollView setContentOffset:contentOffset animated:YES];
}
The UIScrollView function
- (void)scrollRectToVisible:(CGRect)rect
animated:(BOOL)animated
does almost that.
Reference says: "This method scrolls the content view so that the area defined by rect is just visible inside the scroll view. If the area is already visible, the method does nothing."
Sorry, just saw you've already found that function.
Here is the link of a sample project which is doing the same as you want.
This include one class named PagedFlowView.
sample project to center a view in scrollview

iOS UIButton can't click in UIScrollView

I had search some post before. But I can't try out the correct result.
I generate more button and add the view in the UIScrollView.
I had set autolayout in storybaord about the view and scrollview.
But the Button click are not listener. I don't know why.
I had set
_sclVW.delaysContentTouches = NO;
[_sclVW setCanCancelContentTouches:NO];
_sclVW.userInteractionEnabled = YES;
But the button still can't click.
My code is below:
_sclVW.delegate = self;
_sclVW.delaysContentTouches = NO;
[_sclVW setCanCancelContentTouches:NO];
_sclVW.userInteractionEnabled = YES;
int btnWidth = 80,btnHigh=50;
for( int i = 0 ; i < 50 ; i++ )
{
UIButton *button =[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(btnSendTag:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:[NSString stringWithFormat:#"%d",i] forState:UIControlStateNormal];
button.tag = i;
button.titleLabel.numberOfLines = 0;
button.titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
button.titleLabel.textAlignment = NSTextAlignmentCenter;
button.userInteractionEnabled = YES;
[button setEnabled:YES];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor darkGrayColor]];
if( i < 3)
{
button.frame = CGRectMake( i *(btnWidth + 20), 0 , 80,50 );
}
else
{
button.frame = CGRectMake( (i%3) *(btnWidth + 20), (i/3) *(btnHigh + 20) , 80,50 );
}
[_contentVW addSubview:button];
}
}
-(void) btnSendTag:(UIButton *)sender
{
NSLog(#"btn click:%li",(long)sender.tag);
}
Have anyone know how to resolve the problem?
thank you very much.
I had post my simple test project in github(this code).
I found your issue from sample project. Issue here is height and width of contentVW in Storyboard is 0. So that subview (buttons) of this view can not be interact with user.
Look at the picture below:
Here you can see that subview of UIScrollView has value 0 for width and height.
So change width and height of view same like scrollview and than you can able to tap on these buttons.

iOS system button click frame issue

Problem:
Some system UIButton Types are getting clickable outside its defined frame/bounds.
The below two buttons types are getting selected within their defined frame region properly.
UIButtonTypeCustom [width:160, height:44.0]
UIButtonTypeSystem [width:50, height:44.0]
The below button types are even selectable outside of its frame rectangle from all sides by 10~15points.[Background color is drawn/displaying with in the defined frame for all these buttons]
UIButtonTypeDetailDisclosure,[width:50, height:44.0]
UIButtonTypeInfoLight, [width:50, height:44.0]
UIButtonTypeInfoDark, [width:50, height:44.0]
UIButtonTypeContactAdd [width:50, height:44.0]
I expecting the clickable/selectable area should be with in the button frame.
I am creating the buttons using following method:
- (UIButton*)createButton:(UIButtonType)style withFrame:(CGRect)frame {
UIButton *button = nil;
UILabel *label = nil;
switch(style)
{
case UIButtonTypeCustom: // frame: {x,y,160.0,44.0}
button = [[UIButton alloc] initWithFrame:frame];
button.layer.cornerRadius = 10;
label = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 160.0, 44.0)];
label.text = #"Custom";
label.textAlignment = NSTextAlignmentCenter;
[button addSubview:label];
break;
case UIButtonTypeSystem: // frame: {x,y,50.0,44.0}
button = [UIButton buttonWithType:style];
[button setTitle:#"Button" forState:UIControlStateNormal];
button.frame = frame;
break;
default: // frame: {x,y,50.0,44.0}
button = [UIButton buttonWithType:style];
button.frame = frame;
break;
}
// Background whitecolor is displaying properly for all buttons.
button.backgroundColor = [UIColor whiteColor];
return button;
}
The createButton method is called as below
for (int i=0, y=10, x=15; i<6; ++i) {
if(i == 5)
{
y += 90;
x = ([[UIScreen mainScreen] bounds].size.width - 160.0)/2.0;
}
UIButton *btn = [self createButton:5-i withFrame:CGRectMake(x, y, ( i<5 ? 50.0 : 160.0), 44.0)];
[btn setTag: i+1];
[btn addTarget:self action:#selector(settingsView:) forControlEvents:UIControlEventTouchUpInside];
[_subView addSubview:btn];
x += 60;
}
The iOS typically expands the touchable area of buttons according to its own internal rules.
My guess: it makes the buttons easier for users to touch.

UIScrollView needs to get bigger every time I add a button

The scrollView is created in my storyboard and is horizontal. Here is the method where I add a button for every loop and need to increase the size of the scrollView every time a button is added.
- (void) populateGroups {
NSInteger x = [self.groupNameArray count];
for (int i = 0; i < x; i++) {
NSString *name = [self.groupNameArray objectAtIndex:i];
// Create Button
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(count, 0, 100, 54)];
[button setTitle:name forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:10];
[button.layer setBorderWidth:1.0f];
[button.layer setBorderColor:[UIColor whiteColor].CGColor];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button addTarget:self action:#selector(groupPage:) forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
[self.groupScrollView addSubview:button];
count = count + 100;
// Increase ScrollViewSize
self.groupScrollView.frame = CGRectMake(0, 0, frameWidth, 80);
frameWidth = frameWidth + 100;
}
}
The scrollView stays the same when I try to add two buttons to the view.
you have to increase the content size of the scroll view like:
self.groupScrollView.contentSize = CGSizeMake(frameWidth, 80);
and better to do it after the loop.

Adding UIButton to subview not working

I'm trying to add 16 UIButton in code to a subview of my main view:
internView = [[UIView alloc] initWithFrame:CGRectMake(16, 16, (self.view.frame.size.width - 16), (self.view.frame.size.height - 16) - 60)];
internView.backgroundColor = [UIColor whiteColor];
for (int rij = 0; rij < 4; rij++) //4 rows
{
for (int kolom = 0; kolom < 4; kolom++) //4 colomns
{
CGFloat x = (((width + spacing) * kolom) + spacing);
CGFloat y = (((height + spacing) * rij) + spacing);
int tag = ((4 * rij) + kolom);
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, height)];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = [UIColor blackColor];
[button setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[[projects objectAtIndex:tag]thumbnailImage]]]] forState:UIControlStateNormal];
[button addTarget:self action:#selector(getDetail:) forControlEvents:UIControlStateNormal];
button.tag = tag;
NSLog(#"%i",button.tag);
[internView addSubview:button];
}
}
[self.view addSubview:internView];
The subview 'internview' is visible (as i made it white background) on the main view but the buttons aren't visible.
Also my log shows the tag of the button which goes from 0..15, so I know they are being made but simple aren't added to the subview 'internview'
Can't really see what I'm missing or doing wrong here..
Change your code to :
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(x, y, width, height);
In your version you have:
Memory leak because you have created buttons two times
[UIButton buttonWithType:UIButtonTypeCustom]; returns new button
with CGRectZero frame

Resources