I have a strange thing and I would like to fi that.
I have a View which is in landscape mode. Due to landscape mode I have a button which need to change his frame:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 40.0, 160.0);
[view addSubview:button];
Normal:
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
Landscape:
button.frame = CGRectMake(80.0, 210.0, 40.0, 160.0);
All is fine, except the Title of the Button: "SHow View"
Instead of saying Show view it says this:
w
e
i
V
w
o
h
S
read this from button to top :P
This is my button title, because its landscape, but the titles go still for Portrait.
Any help appreciated :)
First of all, it seems you are not setting the rect of your button right. Remember that the third parameter, is the width:
CG_INLINE CGRect
CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height)
{
CGRect rect;
rect.origin.x = x; rect.origin.y = y;
rect.size.width = width; rect.size.height = height;
return rect;
}
In button.frame = CGRectMake(80.0, 210.0, 40.0, 160.0); you are setting the width of your button to 40.
More importantly, you should use strings and struts, or even better, auto layout. Life is so much easier when you let computers do the math. For just one button, springs and struts is all you need, but if you want to create a more flexible interface, and you can target iOS 6, then you should use autolayout.
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
[button setTranslatesAutoresizingMaskIntoConstraints:NO];
[[self view] addSubview:button];
NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:button
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:nil
attribute:NSLayoutAttributeWidth
multiplier:1
constant:200];
[button addConstraint:widthConstraint];
NSLayoutConstraint *bottomContraint = [NSLayoutConstraint constraintWithItem:button
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:[self view]
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:-20];
NSLayoutConstraint *centerContraint = [NSLayoutConstraint constraintWithItem:button
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:[self view]
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0];
NSArray *constraints = [[NSArray alloc] initWithObjects:bottomContraint, centerContraint, nil];
[[self view] addConstraints:constraints];
And if you want to use springs and struts, you have to set the autoresizing mask:
[button setAutoresizingMask:(UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin)];
These two things are much more easier in Interface Builder.
Related
I notice the tap area for my UIButton got smaller since iOS 11. To confirm, I commented out the desired background color on its subviews and put purple color on the button itself.
iOS 10.0.3 (Button shown as desired)
iOS 11.1 (Tap area got smaller)
My code follows.
UIImage *ringImage = [UIImage imageNamed:#"ball20r"];
UIEdgeInsets insets = UIEdgeInsetsMake(10,10,10,10); //padding
UIImage *stretchableImage = [ringImage resizableImageWithCapInsets:insets];
UIImageView *imageView = [[UIImageView alloc]initWithImage:stretchableImage];
// imageView.backgroundColor = backgroundColor; //disabled for testing
imageView.frame = CGRectMake(0, 0, label.frame.size.width + 16.0, label.frame.size.height + 16.0);
[imageView addSubview:label];
label.translatesAutoresizingMaskIntoConstraints = NO;
NSDictionary *viewsDictionary = #{#"label":label};
NSArray *constraint_H = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|-(8)-[label]-(8)-|"
options:0
metrics:nil
views:viewsDictionary];
NSArray *constraint_V = [NSLayoutConstraint constraintsWithVisualFormat:#"H:|-(8)-[label]-(8)-|"
options:0
metrics:nil
views:viewsDictionary];
[imageView addConstraints:constraint_H];
[imageView addConstraints:constraint_V];
UIButton *calendarButton = [UIButton buttonWithType:UIButtonTypeCustom];
[calendarButton addTarget:self action:#selector(chooseACalendarToSave) forControlEvents:UIControlEventTouchUpInside];
calendarButton.frame = imageView.frame;
[calendarButton addSubview:imageView];
//added for testing only
calendarButton.backgroundColor = [UIColor purpleColor];
_calendarButton = [[UIBarButtonItem alloc]initWithCustomView:calendarButton];
How can I make the area for iOS just like iOS 10.0.3 and earlier?
Thanks for your help.
The answer is here.
iOS 11 UIBarButtonItem images not sizing
if (#available(iOS 9, *)) {
[cButton.widthAnchor constraintEqualToConstant: standardButtonSize.width].active = YES;
[cButton.heightAnchor constraintEqualToConstant: standardButtonSize.height].active = YES;
}
As soon as I inserted above code, the button started working as expected. Thank you.
I am trying to replace a set frame with constrains for a UIButton but my code keeps crashing, what am I doing wrong?
- (void)CreateButton {
self.Button = [[UIButton alloc] init];
//self.Button.frame = CGRectMake(30, 30, 100, 100);
[self.Button addConstraint:[NSLayoutConstraint constraintWithItem:MyScrollView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.myButton
attribute:NSLayoutAttributeTop
multiplier:2.0
constant:30]];
[self.myButton addConstraint:[NSLayoutConstraint constraintWithItem:MyScrollView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.myButton
attribute:NSLayoutAttributeHeight
multiplier:3.0
constant:50]];
[self.myButton setBackgroundColor:[UIColor orangeColor]];
[self.myButton setTitle:#"Press Me" forState:UIControlStateNormal];
[self.myButton setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
[MyScrollView addSubview:self.myButton];
You should add the constraint to MyScrollView.
I'm trying to align two Buttons in a View (appearing in cyan). They get aligned vertically in the View. When i try to horizontally align them, their heights get changed. I want their widths should be the same according to the super view's width unlike smaller Next button. Can this be achieved only using VFL?
Here's the code:
[self prepareButton:saveButton label:#"SAVE"];
[buttonContainerView addConstraint:[NSLayoutConstraint constraintWithItem:saveButton
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:buttonContainerView
attribute:NSLayoutAttributeCenterY
multiplier:1.00f
constant:0]];
[saveButton layoutIfNeeded];
[self prepareButton:nextButton label:#"NEXT"];
[buttonContainerView addConstraint:[NSLayoutConstraint constraintWithItem:nextButton
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:buttonContainerView
attribute:NSLayoutAttributeCenterY
multiplier:1.00f
constant:0]];
[nextButton layoutIfNeeded];
[buttonContainerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-[saveButton]-[nextButton]-|"
options:0
metrics:metrics
views:viewDictionary]];
[saveButton layoutIfNeeded];
[nextButton layoutIfNeeded];
-(void)prepareButton:(UIButton *)button
label:(NSString *) label
{
[buttonContainerView addSubview:button];
button.translatesAutoresizingMaskIntoConstraints = NO;
button.backgroundColor = [UIColor redColor];
button.layer.cornerRadius = 2.50f;
[button setTitle:label forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
Solved the issue by setting equal height & assigning priority in VFL:
[buttonContainerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-[saveButton]-[nextButton(==saveButton#1000)]-|"
options:0
metrics:metrics
views:viewDictionary]];
I want to add two buttons to a subview that appears over the parent view when the view will appear. The buttons are initialized correctly and appear but they donĀ“t react when pressed. Does anybody know why? Here is my code:
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.view.backgroundColor = [UIColor blackColor];
//Animation View
imageView= [[UIImageView alloc] initWithFrame:CGRectMake(0, 499, 320, 0)];
imageView.image = [UIImage imageNamed:#"trans.png"];
[imageView setClipsToBounds:YES];
[imageView setContentMode:UIViewContentModeBottom];
[self.view addSubview:imageView];
[UIView animateWithDuration:1.0f
delay:0.5f
options:UIViewAnimationOptionCurveEaseOut
animations:^(void) {
imageView.frame = CGRectMake(0, 92, 320, 320);
}
completion:NULL];
[self.view bringSubviewToFront:imageView];
// Animation View Buttons
//1 Amazon
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *button=[UIImage imageNamed:#"button.png"];
[button1 setImage:button forState:UIControlStateNormal];
button1.frame = CGRectMake(0, 290, 80, 30);
button1.backgroundColor=[UIColor whiteColor];
button1.showsTouchWhenHighlighted=YES;
[button1 addTarget:self
action:#selector(openAmazon:)
forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:button1];
//2 iTunes
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 setImage:button forState:UIControlStateNormal];
button2.frame = CGRectMake(82, 290, 80, 30);
button2.backgroundColor=[UIColor whiteColor];
button2.showsTouchWhenHighlighted=YES;
[button2 addTarget:self
action:#selector(openiTunes:)
forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:button2];
}
you need to use the option UIViewAnimationOptionAllowUserInteraction
...
options:UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionAllowUserInteraction
...
Make sure you set the userInteractionEnabled property on the imageView:
[imageView setUserInteractionEnabled:YES];
[self.view bringSubviewToFront:imageView];
you are bringing your image view to the front after increasing its frame, this is blocking the buttons actions
How do I set the size of a UIButton when instantiating it, ready to use for NSLayoutConstraint?
Code:
UIButtonRaisedSilver *loginButton = [UIButtonRaisedSilver buttonWithType:UIButtonTypeCustom];
[loginButton addTarget:self action:#selector(login) forControlEvents:UIControlEventTouchUpInside];
[loginButton setTitle:#"Log in" forState:UIControlStateNormal];
NSDictionary *views = #{#"tableView" : self.tableView, #"loginButton": loginButton, #"signupButton": signupButton};
[NSLayoutConstraint constraintsWithVisualFormat:#"V:[tableView]-50-[loginButton()]"
options:NSLayoutFormatAlignAllCenterX
metrics:nil
views:views];
It turns out, width and height are also part of the constraints system in iOS6/(OSX 10.8)(i think its 10.8 - havent developed for Mac OS yet).
Make sure the constraint is vertical for height and set the height in the brackets as below...
For example:
[NSLayoutConstraint constraintsWithVisualFormat:#"V:[loginButton(30)]"
options:NSLayoutFormatAlignAllCenterX
metrics:nil
views:views];