I am trying to change a UIButton's width when the view animates to landscape mode. But the method is called because I set a break point there, but the button's width doesn't change. I add a IBOutlet constraint to button's width named: globalButtonWidthConstraint.
My current code :
- (void)layoutSubviews {
[super layoutSubviews];
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
CGFloat screenH = screenSize.height;
CGFloat screenW = screenSize.width;
BOOL isLandscape = !(self.frame.size.width == (screenW*(screenW<screenH))+(screenH*(screenW>screenH)));
if (isLandscape) {
self.globalButtonWidthConstraint.constant = 100;
[self layoutIfNeeded];
} else {
self.globalButtonWidthConstraint.constant = 47;
[self layoutIfNeeded];
}
}
Try updating constraint constant in "viewDidLayoutSubviews".
-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
// Update constraint constant
[self.view layoutSubviews];
}
It worked for me.
EDIT: Make sure that there is no other constraints that conflicting this width constraint.
Related
I have created a custom keyboard and also it supports landscape and portrait orientation.My issue is when i change the orientation keyboard view changes its height and width which happens not like native keyboard.video is added below.I want keyboard to adjust is size like native keyboard.
I am updating keyboard height in viewWillTransitionTSize added below
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
[self updateKeyboardHeight];
}
-(void)updateKeyboardHeight{
if (self.view.frame.size.width == 0 || self.view.frame.size.height == 0)
return;
[self.inputView removeConstraint:self.heightConstraint];
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
CGFloat realScreenHeight = MAX(screenSize.height, screenSize.width);
if([[UIScreen mainScreen] bounds].size.width == realScreenHeight)
{
//Landscape
self.isLandscape = YES;
self.heightConstraint.constant = self.landscapeHeight;
_currentOrientation = LKeyboardButtonLandscape;
[self.view addConstraint:self.heightConstraint];
}
else
{
//Portrait
self.isLandscape = NO;
self.heightConstraint.constant = self.portraitHeight;
_currentOrientation = LKeyboardButtonPortrait;
[self.view addConstraint:self.heightConstraint];
}
}
When I change the height of inputAccessoryView in iOS 8, the inputAccessoryView not go to the right origin, but covers the keyboard.
Here are some code snippets:
in table view controller
- (UIView *)inputAccessoryView {
if (!_commentInputView) {
_commentInputView = [[CommentInputView alloc] initWithFrame:CGRectMake(0, 0, [self width], 41)];
[_commentInputView setPlaceholder:NSLocalizedString(#"Comment", nil) andButtonTitle:NSLocalizedString(#"Send", nil)];
[_commentInputView setBackgroundColor:[UIColor whiteColor]];
_commentInputView.hidden = YES;
_commentInputView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
}
return _commentInputView;
}
in CommentInputView
#when the textview change height
- (void)growingTextView:(HPGrowingTextView *)growingTextView willChangeHeight:(float)height {
if (height > _textView_height) {
[self setHeight:(CGRectGetHeight(self.frame) + height - _textView_height)];
[self reloadInputViews];
}
}
in UIView Category from ios-helpers
- (void)setHeight: (CGFloat)heigth {
CGRect frame = self.frame;
frame.size.height = heigth;
self.frame = frame;
}
Finally, i found the answer. In ios8, apple add a NSContentSizeLayoutConstraints to inputAccessoryView and set a constant with 44. You can't remove this constaint, because ios8 use it to calculate the height of inputAccessoryView. So, the only solution is to change value of this constant.
Example
in ViewDidAppear
- (void)viewDidAppear:(BOOL)animated {
if ([self.inputAccessoryView constraints].count > 0) {
NSLayoutConstraint *constraint = [[self.inputAccessoryView constraints] objectAtIndex:0];
constraint.constant = CommentInputViewBeginHeight;
}
}
change inputAccessoryView height when the textview height changed
- (void)growingTextView:(HPGrowingTextView *)growingTextView willChangeHeight:(float)height {
NSLayoutConstraint *constraint = [[self constraints] objectAtIndex:0];
float new_height = height + _textView_vertical_gap*2;
[UIView animateWithDuration:0.2 animations:^{
constraint.constant = new_height;
} completion:^(BOOL finished) {
[self setHeight:new_height];
[self reloadInputViews];
}];
}
That is.
One way you can update the constraint mentioned in Yijun's answer when changing the height of the inputAccessoryView is by overwriting setFrame: on your inputAccessoryView. This doesn't rely on the height constraint being the first in the array.
- (void)setFrame:(CGRect)frame {
[super setFrame:frame];
for (NSLayoutConstraint *constraint in self.constraints) {
if (constraint.firstAttribute == NSLayoutAttributeHeight) {
constraint.constant = frame.size.height;
break;
}
}
}
The first answer didn't totally solve my problem but gave me a huge hint.
Apple did add a private constraint to the accessory view, but you cannot find it in the constraint list of the accessory view. You have to search for it from its superview. It killed my a few hours.
After reading the answer above, which is a great find, I was concerned that relying on the constraint you need to change being [0] or firstObject is an implementation detail that's likely to change under us in the future.
After doing a bit of debugging, I found that the Apple-added constraints on the accessory input view seem to have a priority of 76. This is a crazy low value and not one of the listed enums in the documentation for priority.
Given this low priority value it seems like a cleaner solution to simply conditionally add/remove another constraint with a high priority level, say UILayoutPriorityDefaultHigh when you want to resize the view?
For Xcode 11.2 and swift 5 this function will update inputAccessoryView constraints even in animation block
func updateInputContainerConstraints() {
if let accessoryView = inputAccessoryView,
let constraint = accessoryView.superview?.constraints.first(where: { $0.identifier == "accessoryHeight" }) {
constraint.isActive = false
accessoryView.layoutIfNeeded()
constraint.constant = accessoryView.bounds.height
constraint.isActive = true
accessoryView.superview?.addConstraint(constraint)
accessoryView.superview?.superview?.layoutIfNeeded()
}
}
Try this:
_vwForSendChat is the input accessory view
_txtViewChatMessage is the textview inside input accessory view
-(void)textViewDidChange:(UITextView *)textView {
CGFloat fixedWidth = textView.frame.size.width;
CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
CGRect newFrame = textView.frame;
newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
if (newFrame.size.height < 40) {
_vwForSendChat.frame = CGRectMake(0, 0, self.view.frame.size.width, 40);
} else {
if (newFrame.size.height > 200) {
_vwForSendChat.frame = CGRectMake(0, 0, self.view.frame.size.width, 200);
} else {
_vwForSendChat.frame = CGRectMake(0, 0, self.view.frame.size.width, newFrame.size.height);
}
}
[self.txtViewChatMessage reloadInputViews];
}
I have subclassed UITextView to make it return an intrinsic content size like this:
- (void) layoutSubviews
{
[super layoutSubviews];
if (!CGSizeEqualToSize(self.bounds.size, [self intrinsicContentSize])) {
[self invalidateIntrinsicContentSize];
}
}
- (CGSize)intrinsicContentSize
{
/*
Intrinsic content size of a textview is UIViewNoIntrinsicMetric
We have to build what we want here: contentSize + textContainerInset should do the trick
*/
CGSize intrinsicContentSize = self.contentSize;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f) {
intrinsicContentSize.width += (self.textContainerInset.left + self.textContainerInset.right ) / 2.0f;
intrinsicContentSize.height += (self.textContainerInset.top + self.textContainerInset.bottom) / 2.0f;
}
return intrinsicContentSize;
}
I have added an observer to the UITextViewTextDidChangeNotification and when the text view content change I update its height to make it growth with the text height:
- (void)textViewTextDidChangeNotification:(NSNotification *)notification
{
UITextView *textView = (UITextView *)notification.object;
[self.view layoutIfNeeded];
void (^animationBlock)() = ^
{
self.messageInputViewHeightConstraint.constant = MAX(0, self.messageInputView.intrinsicContentSize.height);
[self.view layoutIfNeeded];
};
[UIView animateWithDuration:0.3
delay:0
options:UIViewAnimationOptionCurveEaseInOut
animations:animationBlock
completion:nil];
}
But when their is enough lines to fill the text view height, half of the time when a new line is added the NSTextContainer of the UITextView is not well placed like you can see in this picture
(The NSTextContainer is outlined in red and UITextView is outlined in blue)
The other half of the time when a new line is added the NSTextContainer is replaced correctly.
I did not found how to solve this weird behavior.
I hope one of you will have an answer to fix it.
Thank you
I have a UIScrollView with a number of children UITextViews arranged vertically. I want the UIScrollView to resize to fit content. So my TextViews I do
- (void)textViewDidChange:(UITextView *)textView
{
CGFloat fixedWidth = textView.frame.size.width;
CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
CGRect newFrame = textView.frame;
newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
textView.frame = newFrame;
[textView setNeedsLayout];
}
And for the scrollview I do
-(void)resizeScrollViewToFitContent
{
CGRect contentRect = CGRectZero;
for (UIView *view in self.scrollView.subviews) {
contentRect = CGRectUnion(contentRect, view.frame);
}
self.scrollView.contentSize = contentRect.size;
}
I call [self resizeScrollViewToFitContent] inside viewDidAppear. Any ideas why this setup is not working?
I am not sure how is the arrangement of your textViews inside the scrollView. I assume that they are arranged vertically right next to each other. I have created a sample project with a scrollView and 3 textViews and it works well with the following code:-
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
CGFloat height = self.textView1.frame.size.height +self.textView2.frame.size.height +self.textView3.frame.size.height ;
CGSize size = CGSizeMake(self.scrollView.frame.size.width, height);
self.scrollView.contentSize = size;
}
From my own project, the content Height of the scrollview is the combination of the height for all 3 textviews. It might be different in your project.
You shouldn't be doing the union of the contentFrame and view.frame. Try the following:
-(void)resizeScrollViewToFitContent
{
UIScrollView* scrollView;
CGSize contentSize = CGSizeZero;
for (UIView* subview in scrollView.subviews)
{
CGFloat subviewRight = CGRectGetMaxX(subview.frame);
if (subviewRight > contentSize.width)
{
contentSize.width = subviewRight;
}
CGFloat subviewBottom = CGRectGetMaxY(subview.frame);
if (subviewBottom > contentSize.height)
{
contentSize.height = subviewBottom;
}
}
scrollView.contentSize = contentSize;
}
One small thing worth noting: a UIScrollView's scroll indicators are subviews of any UIScrollView. This has the potential to throw off the above code's accuracy, but I've found that it always works except in some very particular cases.
If you know how many subviews are in your scrollview per row then you'll want to do a little math to find the correct height.
For example, you might have a uiview with a uiimageview and uilabel, so you're subview count will be 3 times what it should be.
try
// csv = contentscrollview
NSLog(#"%d", [[csv subviews] count] / 3);
I'm trying to resize a text view according to content & also it's sibling and parent container.
Below code is working fine in iOS 6
if (/* less than ios 7 */) {
CGRect frame = _textView.frame;
CGSize conSize = _textView.contentSize;
CGFloat difference = conSize.height - frame.size.height;
frame.size.height += difference;
_textView.frame = frame;
UIScrollView *parentView = (UIScrollView *)_textView.superview;
// adjust views residing below this text view.
// sibling view
UIView *belowView = // access it somehow
CGRect frame1 = belowView.frame;
frame1.origin.y += difference;
belowView.frame = frame1;
// adjust parent scroll view, increase height.
CGSize frame3 = parentView.contentSize;
frame3.height += difference;
parentView.contentSize = frame3;
} else {
// tried
[_textView sizeToFit];
[_textView layoutIfNeeded];
[parentView sizeToFit];
[parentView layoutIfNeeded];
}
Tried to follow iOS 7 solution from:
How do I size a UITextView to its content on iOS 7?
but not working.
Any pointers?
Working code solution from #NSBouzouki
if (/* ios 7 */) {
[_textView.layoutManager ensureLayoutForTextContainer:_textView.textContainer];
[_textView layoutIfNeeded];
}
CGRect frame = _textView.frame;
CGSize conSize = _textView.contentSize;
CGFloat difference = conSize.height - frame.size.height;
frame.size.height += difference;
_textView.frame = frame;
UIScrollView *parentView = (UIScrollView *)_textView.superview;
// adjust views residing below this text view.
// sibling view
UIView *belowView = // access it somehow
CGRect frame1 = belowView.frame;
frame1.origin.y += difference;
belowView.frame = frame1;
// adjust parent scroll view, increase height.
CGSize frame3 = parentView.contentSize;
frame3.height += difference;
parentView.contentSize = frame3;
It seems UITextView's contentSize property is not correctly set in iOS 7 till viewDidAppear:. This is probably because NSLayoutManager lays out the text lazily and the entire text must be laid out for contentSize to be correct. The ensureLayoutForTextContainer: method forces layout of the provided text container after which usedRectForTextContainer: can be used for getting the bounds. In order to get total width and height correctly, textContainerInset property must be taken into account. The following method worked for me.
- (CGRect)contentSizeRectForTextView:(UITextView *)textView
{
[textView.layoutManager ensureLayoutForTextContainer:textView.textContainer];
CGRect textBounds = [textView.layoutManager usedRectForTextContainer:textView.textContainer];
CGFloat width = (CGFloat)ceil(textBounds.size.width + textView.textContainerInset.left + textView.textContainerInset.right);
CGFloat height = (CGFloat)ceil(textBounds.size.height + textView.textContainerInset.top + textView.textContainerInset.bottom);
return CGRectMake(0, 0, width, height);
}
Additionally, it seems UITextView's setContentSize: method is called from layoutSubviews. So, calling layoutIfNeeded on a textView (which itself calls layoutSubviews) after calling ensureLayoutForTextContainer: on its layoutManager, should make the textView's contentSize correct.
[someTextView.layoutManager ensureLayoutForTextContainer:someTextView.textContainer];
[someTextView layoutIfNeeded];
// someTextView.contentSize should now have correct value
GrowingTextViewHandler is an NSObject subclass which resizes text view as user types text.
Here is how you can use it.
#interface ViewController ()<UITextViewDelegate>
#property (weak, nonatomic) IBOutlet UITextView *textView;
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightConstraint;
#property (strong, nonatomic) GrowingTextViewHandler *handler;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.handler = [[GrowingTextViewHandler alloc]initWithTextView:self.textView withHeightConstraint:self.heightConstraint];
[self.handler updateMinimumNumberOfLines:3 andMaximumNumberOfLine:8];
}
- (void)textViewDidChange:(UITextView *)textView {
[self.handler resizeTextViewWithAnimation:YES];
}
#end