I want to change the height of keyboard in XCode 6 Beta 5. I searched code and found that by using NSLayoutConstraint, we can change the height of it but not work for me.
This is my code:
CGFloat _expandedHeight = 500;
NSLayoutConstraint *_heightConstraint =
[NSLayoutConstraint constraintWithItem: self.view
attribute: NSLayoutAttributeHeight
relatedBy: NSLayoutRelationEqual
toItem: nil
attribute: NSLayoutAttributeNotAnAttribute
multiplier: 0.0
constant: _expandedHeight];
[self.view addConstraint: _heightConstraint];
In order for this to work all the views that are added to the UIInputViewController's view need to use layout constraints so you can't add any subviews that use UIViewAutoresizing masks. If you want to use UIViewAutoresizing just add a subview like below then add all of your other views to that view.
UIView *mainView = [[UIView alloc] initWithFrame:self.view.bounds];
[mainView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:mainView];
NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:mainView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0.0];
NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:mainView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0.0];
[self.view addConstraints:#[widthConstraint, heightConstraint]];
Related
I'm trying to put a simple button within the AVPlayerViewController (for my tvOS app) in the bottom left corner of the screen above the scrub bar. I'm getting an error that indicates "Constraint items must each be a view or layout guide".
I don't have the positioning set correctly as I'm just trying to see if I can get the button to show up at all. Can someone advise why this code is crashing?
- (void)setupBreakButton
{
NSLog(#"**** In setupBreakButton....");
UIButton* skipBreakButton = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
skipBreakButton.backgroundColor = [[UIColor bm_colorWithHexString:#"64A6BD"] colorWithAlphaComponent:1.0f];
skipBreakButton.layer.cornerRadius = 6;
skipBreakButton.translatesAutoresizingMaskIntoConstraints = NO;
/* Leading space to superview */
NSLayoutConstraint *leftButtonXConstraint = [NSLayoutConstraint
constraintWithItem:skipBreakButton.leadingAnchor attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual toItem:self attribute:
NSLayoutAttributeLeft multiplier:1.0 constant:30];
/* Top space to superview Y*/
NSLayoutConstraint *leftButtonYConstraint = [NSLayoutConstraint
constraintWithItem:skipBreakButton.topAnchor attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual toItem:self attribute:
NSLayoutAttributeTop multiplier:1.0f constant:258];
/* Fixed width */
NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:skipBreakButton.widthAnchor
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:35];
/* Fixed Height */
NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:skipBreakButton.heightAnchor
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:12];
/* 4. Add the constraints to button's superview*/
[self.view addConstraints:#[leftButtonXConstraint, leftButtonYConstraint, widthConstraint, heightConstraint]];
}
Also, how do I add this button as a subview of the AVPlayerViewController?
Inside your custom viewController call this method
UIButton* skipBreakButton;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSURL *url = [[NSURL alloc] initWithString:#""];
AVPlayer *player = [AVPlayer playerWithURL:url];
AVPlayerViewController *controller = [[AVPlayerViewController alloc] init];
[self addChildViewController:controller];
[self.view addSubview:controller.view];
controller.view.frame = CGRectMake(0,0,UIScreen.mainScreen.bounds.size.width,UIScreen.mainScreen.bounds.size.height);
controller.player = player;
controller.showsPlaybackControls = YES;
[player play];
NSLog(#"**** In setupBreakButton....");
skipBreakButton = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
skipBreakButton.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:1.0f];
skipBreakButton.layer.cornerRadius = 6;
[skipBreakButton addTarget:self action:#selector(didTapButton:) forControlEvents:UIControlEventPrimaryActionTriggered];
[self.view addSubview: skipBreakButton];
skipBreakButton.translatesAutoresizingMaskIntoConstraints = NO;
/* Leading space to superview */
NSLayoutConstraint *leftButtonXConstraint = [NSLayoutConstraint
constraintWithItem:skipBreakButton attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual toItem:self.view attribute:
NSLayoutAttributeLeading multiplier:1.0 constant:30];
/* Top space to superview Y*/
NSLayoutConstraint *leftButtonYConstraint = [NSLayoutConstraint
constraintWithItem:skipBreakButton attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual toItem:self.view attribute:
NSLayoutAttributeTop multiplier:1.0f constant:258];
/* Fixed width */
NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:skipBreakButton
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:35];
/* Fixed Height */
NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:skipBreakButton
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:12];
/* 4. Add the constraints to button's superview*/
[self.view addConstraints:#[leftButtonXConstraint, leftButtonYConstraint, widthConstraint, heightConstraint]];
}
- (NSArray<id<UIFocusEnvironment>> *)preferredFocusEnvironments {
return #[skipBreakButton];
}
-(void)didTapButton:(UIButton *) sender {
NSLog(#"didtapbutton");
}
I need to create a UISlider and put it above an existing slider.
I do know how to create constraints for a view if I want to attach it to its superview:
UIView *superview = view.superview;
[view setValue: [NSNumber numberWithBool: FALSE] forKey: #"translatesAutoresizingMaskIntoConstraints"];
NSLayoutConstraint *topConstraint =[NSLayoutConstraint
constraintWithItem: view
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem: superview
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0];
NSLayoutConstraint *bottomConstraint =[NSLayoutConstraint
constraintWithItem: view
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem: superview
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0.0];
NSLayoutConstraint *leadingConstraint =[NSLayoutConstraint
constraintWithItem: view
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem: superview
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0.0];
NSLayoutConstraint *trailingConstraint =[NSLayoutConstraint
constraintWithItem: view
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem: superview
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:0.0];
NSArray *constraints = #[topConstraint, bottomConstraint, leadingConstraint, trailingConstraint];
[superview addConstraints: constraints];
But the same method does not work when I need to attach two subviews together. Say, I have view1 as subview of superview. It has been created some time ago. Now I need another one (view2) to have the same positioning inside superview.
Something like
UIView *superview = view1.superview;
UIView *view2 = [[UIView alloc] init];
[superview addSubview: view2];
[view2 setValue: [NSNumber numberWithBool: FALSE] forKey: #"translatesAutoresizingMaskIntoConstraints"];
NSLayoutConstraint *topConstraint =[NSLayoutConstraint
constraintWithItem: view1
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem: view2
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0];
NSLayoutConstraint *bottomConstraint =[NSLayoutConstraint
constraintWithItem: view1
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem: view2
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0.0];
NSLayoutConstraint *leadingConstraint =[NSLayoutConstraint
constraintWithItem: view1
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem: view2
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0.0];
NSLayoutConstraint *trailingConstraint =[NSLayoutConstraint
constraintWithItem: view1
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem: view2
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:0.0];
NSArray *constraints = #[topConstraint, bottomConstraint, leadingConstraint, trailingConstraint];
[superview addConstraints: constraints];
breaks everything.
Looks like you just have it backward...
You want to constrain view2 to view1:
[NSLayoutConstraint
constraintWithItem: view2 // <-- constrain this view
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem: view1 // <-- to this view
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0];
I'm trying to get a UIButton added to a TableView, where it sits in the bottom right corner of the table view, 20 from the right edge, and 20 from the bottom. However, it ends up sticking the button at the top left edge. What am I doing wrong?
UIButton *goToTop = [UIButton buttonWithType:UIButtonTypeCustom];
[goToTop setImage:redGo forState:UIControlStateNormal];
[goToTop addTarget:self action:#selector(beginCampaign) forControlEvents:UIControlEventTouchUpInside];
[goToTop setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[goToTop.layer setBorderColor:[[UIColor redColor] CGColor]];
[self.tableView addSubview:goToTop];
goToTop.translatesAutoresizingMaskIntoConstraints = NO;
/* Leading space to superview */
NSLayoutConstraint *leftButtonXConstraint = [NSLayoutConstraint
constraintWithItem:goToTop attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual toItem:self.tableView attribute:
NSLayoutAttributeLeft multiplier:1.0 constant:20];
/* Top space to superview Y*/
NSLayoutConstraint *leftButtonYConstraint = [NSLayoutConstraint
constraintWithItem:goToTop attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual toItem:self.tableView attribute:
NSLayoutAttributeTop multiplier:1.0f constant:20];
/* Fixed width */
NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:goToTop
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:60];
/* Fixed Height */
NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:goToTop
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:60];
/* 4. Add the constraints to button's superview*/
[self.tableView addConstraints:#[leftButtonXConstraint, leftButtonYConstraint, widthConstraint, heightConstraint]];
Here is how I would like it:
Here is how it appears:
You have set your constraints to the top left of the table view not the bottom right.
Change this:
/* Leading space to superview */
NSLayoutConstraint *leftButtonXConstraint = [NSLayoutConstraint
constraintWithItem:goToTop attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual toItem:self.tableView attribute:
NSLayoutAttributeLeft multiplier:1.0 constant:20];
/* Top space to superview Y*/
NSLayoutConstraint *leftButtonYConstraint = [NSLayoutConstraint
constraintWithItem:goToTop attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual toItem:self.tableView attribute:
NSLayoutAttributeTop multiplier:1.0f constant:20];
To this:
/* Leading space to superview */
NSLayoutConstraint *leftButtonXConstraint = [NSLayoutConstraint
constraintWithItem:goToTop attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual toItem:self.tableView attribute:
NSLayoutAttributeRight multiplier:1.0 constant:20];
/* Top space to superview Y*/
NSLayoutConstraint *leftButtonYConstraint = [NSLayoutConstraint
constraintWithItem:goToTop attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual toItem:self.tableView attribute:
NSLayoutAttributeBottom multiplier:1.0f constant:20];
I'm trying to put a custom button into UIImagePickerController using auto layout constraints.
- (void)createTimerButtonFor:(UIImagePickerController*)picker
{
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:#"Timer" forState:UIControlStateNormal];
NSLayoutConstraint* constraint1 = [NSLayoutConstraint constraintWithItem:picker.view attribute:NSLayoutAttributeCenterX relatedBy: NSLayoutRelationEqual toItem:button attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
NSLayoutConstraint* constraint2 = [NSLayoutConstraint constraintWithItem:picker.view attribute:NSLayoutAttributeTop relatedBy: NSLayoutRelationEqual toItem:button attribute:NSLayoutAttributeTop multiplier:1 constant:0];
NSLayoutConstraint *heightConstraint =
[NSLayoutConstraint constraintWithItem:button
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:100.0];
NSLayoutConstraint *widthConstraint =
[NSLayoutConstraint constraintWithItem:button
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:100.0];
[picker.view addSubview:button];
[picker.view addConstraint:constraint1];
[picker.view addConstraint:constraint2];
[button addConstraint:heightConstraint];
[button addConstraint:widthConstraint];
}
Is it even possible to do so? Am I doing something wrong?
Dzior, I think it would be quite hard to add the button to the picker view and let autolayout to position it for you. I guess maybe it is not fully using the auto layout.
I have tried to disable it from rotating but all the tricks for before iOS 8 is not working properly on iOS 8.
It will be easier to create an overlay view and put your button in the overlay view because you have full control of the overlay view class.
I have an image view that I have lined up on the center right of my screen. All is fine when adding the image view by itself with constraints. I need to animate the image view from that original position(Point A) to the bottom portion(Point B) of the screen; the problem is when I try to animate from Point A to Point B, the image view starts off in the upper left hand corner of the screen, like it was simply added with no constraints (as opposed to where I want it starting off in the center) .
Here is the code and some comments:
#define DEGREES_TO_RADIANS(x) (M_PI * x / 180.0) //defined above
- (void)viewDidLoad
{
UIImage *cardFaceDownImage = [UIImage imageNamed:#"b-top#2x.png"];
UIImageView *dealCardDown = [[UIImageView alloc] initWithImage:cardFaceDownImage];
dealCardDown.translatesAutoresizingMaskIntoConstraints = NO;
NSLayoutConstraint *dealCardConstraintY = [NSLayoutConstraint constraintWithItem:dealCardDown attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:1];
NSLayoutConstraint *dealCardConstraintX = [NSLayoutConstraint constraintWithItem:dealCardDown attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:130];
[self.view addConstraint:dealCardConstraintY];
[self.view addConstraint:dealCardConstraintX];
[self.view addSubview:dealCardDown];
//IF I comment out the below code the UIImageView lines up where I want it (Point A)
//IF I don't coment it out, the imageview starts in the upper left hand corner of the screen
//where it normally would as if just adding an image view programatically without
//constraints would, but it does end up where I want it to (Point B)
[self.view removeConstraint:dealCardConstraintX];
[self.view removeConstraint:dealCardConstraintY];
dealCardConstraintY = [NSLayoutConstraint constraintWithItem:dealCardDown attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:1];
dealCardConstraintX = [NSLayoutConstraint constraintWithItem:dealCardDown attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1 constant:130];
[self.view addConstraint:dealCardConstraintY];
[self.view addConstraint:dealCardConstraintX];
[self.view setNeedsUpdateConstraints];
[UIView animateWithDuration:2.0 delay:2.0 options:UIViewAnimationCurveEaseIn animations:^(void)
{
dealCardDown.transform = CGAffineTransformRotate(dealCardDown.transform, DEGREES_TO_RADIANS(90));
[self.view layoutIfNeeded];
}
completion:^(BOOL finished)
{
}
];
}
..
You need to move change of constraint into animation block, so it will be animated properly.
This code works for me well.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
UIView *dealCardDown = [[UIView alloc] initWithFrame:(CGRect){0,0,50,50}];
dealCardDown.backgroundColor = [UIColor redColor];
dealCardDown.translatesAutoresizingMaskIntoConstraints = NO;
NSLayoutConstraint *sizeConstraint = [NSLayoutConstraint constraintWithItem: dealCardDown attribute: NSLayoutAttributeWidth relatedBy: NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributeNotAnAttribute multiplier: 1 constant: 50];
[dealCardDown addConstraint:sizeConstraint];
sizeConstraint = [NSLayoutConstraint constraintWithItem: dealCardDown attribute: NSLayoutAttributeHeight relatedBy: NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributeNotAnAttribute multiplier: 1 constant: 50];
[dealCardDown addConstraint:sizeConstraint];
[self.view addSubview:dealCardDown];
NSLayoutConstraint *dealCardConstraintX = [NSLayoutConstraint constraintWithItem:dealCardDown attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.f constant:0.f];
NSLayoutConstraint *dealCardConstraintY = [NSLayoutConstraint constraintWithItem:dealCardDown attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.f constant:-self.view.center.y+dealCardDown.frame.size.height/2];
[self.view addConstraint:dealCardConstraintX];
[self.view addConstraint:dealCardConstraintY];
[self.view layoutIfNeeded];
[UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationCurveEaseIn animations:^(void)
{
dealCardDown.transform = CGAffineTransformRotate(dealCardDown.transform, 90*M_PI/180);
dealCardConstraintY.constant = 0;
[self.view layoutIfNeeded];
}
completion:^(BOOL finished)
{
}
];
}