Set the height of a UITextView from a UIViewController's viewWillAppear event - iphone

I'm trying to set the height of a UITextView from a UIViewController's viewWillAppear event, here's the code:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
textView.text = blah blah...
textView.frame = CGRectMake(textView.frame.origin.x, textView.frame.origin.y,
textView.frame.size.width, textView.frame.size.height-20);
}
However, it seems to ignore my sizing, it's as though after this event is called, the textview is resized by something else.
What am i doing wrong? Is there some good way to do this?
Thanks
-edit-
Got it working by putting the code in my viewDidAppear instead of the viewWillAppear.
So it works now.
Although i'm puzzled why i need to do this (remove 20 pixels from the bottom) - i mean shouldn't it resize to fill automatically? Why is it resizing to 20px too long? There's a tab bar at the bottom and a nav bar up the top. Is that confusing it?

Simple but obvious question, does the text change, in other words, is the outlet hooked up in IB?
JUSTA TIP: Look at using the helper functions for CGRect such as:
textView.frame = CGRectInset(textView.frame, 0, 20);

In an app with just this code and a single TextView the resizing occurs as expected. So..it's definitely something else in your app.
Are you maybe referencing the contentsize property or something else in your viewDidAppear?

Got it working by putting the code in my viewDidAppear instead of the viewWillAppear.
So it works now.
Although i'm puzzled why i need to do this (remove 20 pixels from the bottom) - i mean shouldn't it resize to fill automatically? Why is it resizing to 20px too long? There's a tab bar at the bottom and a nav bar up the top.

Related

Storyboard UIScrollView contentSize?

I feel like I have touched on every single possible cause for stopping this, but I have a UIScrollView in my Storyboard hooked up with an outlet and in the viewDidLoad I set the contentSize so that I can scroll (yes bigger than my frame size)!
However, whatever I change, I just can't scroll! I have a couple of textfields in my scrollview and bouncing enabled so I can see that when testing its moves up and down with my subviews in it but whatever I set the contentSize to I just can't scroll.
Anything I might be missing/should check? Is this a known issue with UIScrollView being used in a storyboard?
Whats even stranger is, I can do something like this:
[scrollView setBackgroundColor:[UIColor blueColor]]; and I have a blue scroll view! But setting content size fails.
Edit
My only code (otherwise scrollview is just dropped into storyboard view controller):
-(void)viewDidAppear:(BOOL)animated
{
[scrollView setContentSize:CGSizeMake(320, 640)];
}
Logged frame, comes out as expected:
width: 320.00
height: 504.00
Edit 2
Turns out that removing any subviews of the scroll view in my storyboard lets it scroll just fine. If I add any subview to it at all via the storyboard, even a blank brand new UIButton it just won't apply the contentSize/allow scrolling.
use ViewDidLayoutSubview
- (void)viewDidLayoutSubviews
{
[_scrollView setContentSize:CGSizeMake(320, 500)];
}
UIViewController's method invoke sequence is as below
awakeFromNib
viewDidLoad
viewWillAppear
viewWillLayoutSubviews
viewDidLayoutSubviews
viewDidAppear
viewDidLoad is not a good place to put code that relies on frame sizes of IB objects. If you log the contentSize of your scroll view in viewDidLoad, you will see that it's (0,0). Move the code (where you set the content size) to viewDidAppear, and it will work properly.
Check these
User Interaction enabled
Outlet connected
Included contentsize greater than bounds
scrolling Enabled
eg
scrollView.contentSize = CGSizeMake(320, 640);
My storyboard looks like this for scrollview [working]
I had exactly the same line of code in viewDidAppear and it did not work
Moved it to viewDidLayoutSubviews and it worked correctly.
[scrollView setContentSize:CGSizeMake(320, 500)];
Thanks trick14 for the answer.
The issue is most probably with Auto Layout. UIScrollView needs special attention when using AutoLayout.
Quick-fix - bind one of the scroll's subviews to the top AND bottom space of it's superview (the scroll view).
Long story:
Questions on SO:
UIScrollView not scrolling regardless of large contentSize,
UIScrollView will not scroll, even after content size set,
UIScrollView doesn't use autolayout constraints
Apple's Documentation:
https://developer.apple.com/library/ios/technotes/tn2154/_index.html
Trip14's answer worked for me. In swift I coded it as:
override func viewDidLayoutSubviews() {
(self.view as! UIScrollView).contentSize = CGSizeMake(600, 600)
}
This seems to be a similar issue. Other Story
It might be an issue with auto layout or constraints in the storyboard.
the best way with the storyboard.:

MKMapView inside a UIScrollView doesn't move with swipes

I've got a detail view with various labels and such providing information about a place (address, phone, etc.). The information provided is taller than an iPhone screen so they're all in a UIScrollView (itself inside a UIView) that allows you to swipe up and down to see everything.
I also have an MKMapView inside the scrollview. When it's not attached to anything in Interface Builder it moves up and down with the scrollview, as it should, staying in it's correct relative position to the other controls in the scrollview. You can play with the map, zooming and panning it, etc. and it shows your current location by default.
However, as soon as I hook it to an MKMapView variable in IB, the mapview no longer scrolls with the scrollview. Instead it literally just sits in the position it's originally displayed in (bottom of the view, with a little of the map hidden below the bottom of the view) and the scrollview scrolls up and down behind it.
What's happening here? I've tried changing a bunch of the mapview's and scrollview's properties in IB, but it has no effect. The only thing I haven't tried is just creating the mapview entirely in code, but that doesn't seem like an obvious solution.
EDIT: Sorry to everyone about the expired bounty. I got hung up in other areas of the project and couldn't get back here until now. I didn't know it would expire.
FURTHER EDIT: Well, I figured out my problem. For reasons completely unknown to me I had
[self.view addSubview:mapView];
in the viewcontoller's ViewDidLoad. Once it was hooked up then that line of code would (obviously) make the map a subview of my of view, effectively yanking it out of the scrollview.
Stupid mistake, sorry to have wasted your time (and the bounty). I'll delete this question after I think the answerers have had a chance to see the result.
Looking like as you are using the ScrollView,you need to scrolling facility in your DetailView.
Instead of using the ScrollView ,I had an alternative of this ....
You can try your hard luck by using the TableView instead of ScrollView.
Just take all the labels and mapView in a single View and then put that view in the header of the TableView.
like this :
UITableView
--> View
------>All Labels // Inside the singleView
------>MKMApView // At bottom of the View
Still You can play with the map, zooming and panning it, etc. and it will show your current location by default.
Hope this alternative can solve your problem.......
All the Best
If hooking up an outlet in IB is breaking an otherwise working view, you might be able to try this to locate the view at runtime:
- (UIView *) findClass:(Class) aClass inView:(UIView *) aSuperview {
for ( UIView *view in aSuperview.subviews ) {
if ( [view isKindOfClass: aClass] ) break;
if ( ( view = [self findClass: aClass inView: aSuperview] ) ) break;
}
return view;
}
- (void) viewDidLoad {
MkMapView *map = [self findClass: [MkMapView class] inView: self.view];
}
I figured out my problem. For reasons completely unknown to me I had
[self.view addSubview:mapView];
in the viewcontoller's ViewDidLoad. Once it was hooked up then that line of code would (obviously) make the map a subview of my of view, effectively yanking it out of the scrollview.
Do you have setContentSize property set to the content's size in the viewDidLoad method of the UIViewController?

ipad iphone repositioning uinavigation bar, moving it down to have a logo

Does anyone know if the uinvagitionbar of a uinavigationcontroller can be moved down? I'd like to move it around 200 pixels down to have a logo on top. From my research, I understand that the navigationbar should not be subclassed and there are only two properties that should be changed, it's color and it's visibility. So is this impossible?
I tried moving it's frame, but to no avail.
I've seen other apps do it, but I'm thinking it might be a toolbar? Can the toolbar be repositioned?
Thanks
Just change the size of the frame of the navigations controller's view.
CGRect frame = navigationController.view.frame;
frame.size.height -= 200.0f;
frame.origin.y += 200.0f;
navigationController.view.frame = frame;
You can then add whatever view you'd like to the view or window that contains the navigationController's view.
There a few tricky things to consider if you plan on doing this by presenting a modal view controller, however.
You can hide the self.navigationController and put another navigation bar in the code which will move according to your frame set.
Yes the tool bar can be repositioned. Just take a control in your code and set its frame as per your requirement.
Happy Coding...

UIScrollView not showing scroll indicator

I have a UIScrollView which I create and size dynamically using...
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width , length);
I then add subviews to the UIScrollView.
I do have scrollView.showsVerticalScrollIndicator = YES;
When scrolling the scroll indicator never appears.
Even if I call [scrollView flashScrollIndicators] nothing happens.
Ideas?
Had the same problem and couldn't find the cause. The last answer gave the final hint: whenever I added new subviews I first removed all existing subviews from the scrollview (and apparently also the scroll indicators).
After checking in my loop, if the subview really was of the kind I wanted to be removed the scroll indicators showed up:
for (NSObject * subview in [[[scrollView subviews] copy] autorelease]) {
if ([subview isKindOfClass:[MySubView class]]) {
[(MySubView*)subview removeFromSuperview];
}
}
Update: changed code to Nikolai's suggestion
When I've dealt with this before, in my implementation of a grid, I would occasionally get some cells over the top of the scroll indicator. To fix this I am now inserting subviews at index 0 rather than adding them, which adds them to the top. So try something like this:
[scrollview insertSubview:subview atIndex:0];
For me, the horizontal indicator had mysteriously disappeared in my app on iOS 7. Later found out that for some strange reason, I had to enable both Shows Horizontal Indicator and Shows Vertical Indicator to make the horizontal one show up. If I set it to not show the vertical indicator, it would also not show horizontal indicator.
I fix this by adding this code after add new subview:
self.showsVerticalScrollIndicator = NO;
self.showsVerticalScrollIndicator = YES;
It will also happen (at least in the case of a UITableView) if the contentSize is too small for the table view to scroll. If you have enabled bouncing, then the tableview does not actually scroll and does not display the indicators therefore. Try fitting more content inside.
It can happen also if the parent of the scrollview is smaller horizontally than the scroll view itself :
The scroll bar is stuck to the right side of the ScrollView / TableView and this right side is not visible due to the parent bounds ( with a clipToBounds hidding it for instance).
I've seen this issue so I share it in case it can help.
Just check the width of your ScrollView's frame not to be bigger than the width of its parent view frame.
Two conditions,
If you are using a storyboard
If you are using a UITableView inside a UIViewController
Then, you should check your indicator insets are set to 0 (or any other number that is relevant to your autolayout):
Noticed this when the UIScrollView was a 48 px tall horizontal band, scrollable horizontally. Maybe Cocoa decides the area is too small for a scroll indicator...

Get correct bounds for navigationItem.titleView layoutSubviews

I have a subclass of UIView that I've added to as the titleView of a navigationItem using the following line of code:
self.navigationItem.titleView = tempview;
Easy enough. That works fine. My problem is that this navigationItem sometimes has the rightBarButton updated (sometimes there is no button, sometimes there is one standard sized button, sometimes there is a larger button).
I figured that I could simply use the layoutSubviews method of the tempview class that I've added as the titleView so I put this in:
-(void)layoutSubviews {
[super layoutSubviews];
self.mylabel.frame = self.bounds;
}
This does not seem to work, as it does not correctly resize the titleview when the rightBarButton item is updated.
I've noticed also that the bounds do not grow once they gotten smaller, they simply change the position.
I've tried using setNeedsLayout and layoutIfNeeded but those simply "resize" the view with the incorrect bounds.
I've also made sure that the rightBarButton item is set to nil, but the view still does not correctly expand once shrunk.
Thanks for any help!
configure your view with
[self setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];
(probably in the initWithFrame)
then implement
- (void)willMoveToSuperview:(UIView *)newSuperview
{
[self setFrame:[newSuperview bounds]];
}
now you have your view matching the size of the container, and resizing automatically.
By default, layoutSubviews does nothing. You also never change the size of the titleView, so unless a navbar does that for you, it's no surprise that nothing is changing.
Since you're only changing the size of one of the subviews, I don't think autoresize will work, as I'm pretty sure it's triggered when the superview (the one with autoresizesubviews enabled) changes size. I would suggest recalculating the size of the titleview when you change the rightbutton. If it automatically fits when you add it the first time, you could remove and readd it, but I know that's a pretty ugly hack.
Instead of overriding -layoutSubviews have you tried setting the autoresizingMask property?
tempView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;