I have set a UIView in my storyboard and make it an outlet.
#property (nonatomic, weak) IBOutlet UIView *testView;
In the - (void)viewDidLoad method, I want to change its frame like this
CGRect frame = self.testView.frame;
frame.size.height = 2;
self.testView.frame = frame;
But it does not work. Anybody can tell me why?
You might enabled the autolayout in your project , if so the setFrame straightly won't work . Check here for more info
Solution :
Disable the autolayout, if you don't need any more
Check the above link using with the autolayout .
Look at the tiny circle to the left of the property. Is it filled in or hollow? If it's hollow, then it means that you have not connected this particular element of the storyboard to the file's owner.
If the circle is filled in, than the outlet IS connected to something on your storyboard. Just make sure it's connected to the right entity, namely the UIViewController (or subclass thereof) that you are declaring testView within.
Assuming its connected to your XIB do the following after changing the view -
[self.view setNeedsDisplay];
Trying changing the height to high number and see if that make a difference. I have created demo project for changing and reverting the height. Hope this helps.
https://github.com/rshankras/StackOverflowUIView
Related
My app displays banner ads at the bottom of the screen.
Above these ads are several UIViews/UIImageViews etc.
If the user removes the ads, via IAP, there is an empty gap where the banner used to be, as expected.
However, I would like to move these views down vertically, so as to reduce the obvious gap in the screen.
The code I've been trying doesn't seem to be working -
UIView *moveView = [self.view viewWithTag:5];
CGRect frame = moveView.frame;
[moveView removeFromSuperview];
frame.origin.y = frame.origin.y + 25.0;
moveView.frame = frame;
[self.view addSubview:moveView];
The answer below suggested to remove autolayout, however I needed autolayout for my storyboard.
I simply needed to remove the autolayout for one UIView.
What I ended up using was
[moveView settranslatesautoresizingmaskintoconstraints:YES]
This simple line allowed me to move my view around without worrying about autolayout.
You can create an IBOutlet and connect it to a UIView in storyboard.
#property (nonatomic, retain) IBOutlet UIView *moveView;
then turn off auto layout in your storyboard.
Once that is done then try your code.
or try this
[moveView setFrame:CGRectMake(0,0,320,480)];
mmmbaileys is right.!!
Small Change:
[self.yourview setTranslatesAutoresizingMaskIntoConstraints:YES];
use this in viewDidLoad.
I originally had a normal view with a bunch of stuff in it, and I want that view to be an UIScrollView, so I created a new ScrollView and dragged everything over. The view is not scrolling.
After looking around - I unchecked use autolayout, but that didn't work. I also realize that this could be solved by setting contentSize, but I have access to this view through a variable that is of type UIView, and not UIScrollView. In other words I would be doing -
self.someController.view.contentSize = //something
where self.someController.view is only an UIView and contentSize is not a property of UIView(or at least that's what I'm seeing- I get compiler warnings).
Why is this scroll view not scrolling?
EDIT -
So I stupidly forgot I can cast - I did that, but it's still not working -
UIScrollView* basicCardView = ((UIScrollView *)self.detailController.view);
basicCardView.contentSize = CGSizeMake(basicCardView.frame.size.width * 12,
basicCardView.frame.size.height);
You need to connect your scrollview to Iboutlet var in .m .
Create var in your interface:
IBOutlet UIScrollView *scrollview;
And connect to your scrollview in storyboard, then you can set contentsize.
[scrollview setContentSize:CGSizeMake(2000,200)];
Elto has it correct. To elaborate: In your view controller .m say it's called MyViewController:
#interface MyViewController ()
#property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
#end
Go back to storyboard, select your view controller and select the connections tab (last one on the upper right). You should now see an outlet called scrollView. Drag a connection from that outlet to the scroll view that you added to the scene.
Now the content size code can be written. Just to get it working, use large constants in content size, like:
self.scrollView.contentSize = CGSizeMake(1000, 1000);
The size you're setting in the post (on the wrong view) looks like it's just the size of one of the subviews. You want the content size of the scroll view to be more like a multiple of the scrollView's size. You want the rectangle it can scroll within to at least encompass the frame (not the bounds) of every subview it contains.
I got a weird bug. I have a UIViewController with a UIScrollView embedded inside the UIView. Inside the UIScrollview are few images, but I need more space, so I want to put some images under the current ones. I did all the usual stuff to create and initialize my ScrollView, but instead of scrolling in the UP/DOWN direction, it scrolls LEFT/RIGHT … What is going on?
I declared "UIScrollViewDelegate" in the .h
My property #property (weak, nonatomic) IBOutlet UIScrollView *scrollViewContent; is connected to my scrollview in IB
My ContentSize is declared as: self.scrollViewContent.contentSize= CGSizeMake(self.scrollViewContent.frame.size.width, 1000); and whether I declare it in ViewDidLoad or viewDidLayoutSubviews doesn't change anything.
I set self.scrollViewContent.delegate = self;
in ViewDidLoad
UPDATE
Interestingly enough, I tried setting the ContentSize and then printing it to the console. As it turns out, the "Height" attribute always returns 0. Also, note that I set the "Width" to 1500, but it prints 2000. I'm not using auto-layout.
[self.scrollViewContent setContentSize:CGSizeMake(1500, 2000)];
NSLog(#"contentSize width %f", self.scrollViewContent.contentSize.width);
NSLog(#"contentSize height %f", self.scrollViewContent.contentSize.height);
Result:
"contentSize width 2000.000000"
"contentSize height 0.000000"
UPDATE 2 If I take the exact same Code and the exact same Xib in another project, it works just fine… what is wrong with my actual project?
Please cross check that you declared
UIScrollViewDelegate at .h file or not..
At .m File
#synthesize scrollViewContent;
Hope it may help you...
I have a ViewController Container that is 400 high on the iphone5. When I use it on a iphone4 it squashes the view like it does to everything. In this case I want the view to appear the same size on all phones.
vc.h
#property (strong, nonatomic) IBOutlet UIView *viewForContainer;
vc.m
#synthesize viewForContainer
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect rect = [viewForContainer frame];
rect.size.height = 400;
[viewForContainer setFrame:rect];
}
Why can't I just force the height like this?
.. am I going to have to make a new storyboard for it?
Short answer:
Select the view and choose Editor > Pin > Height. Now get rid of the unnecessary bottom constraint.
Long answer:
You really should stop everything you're doing and understand what you're doing - in particular, about autolayout. It's everywhere, by default, whether we like it or not, so one may as well know about it.
I'm re-writting an app under IOS6 with autolayout but have problems accessing the size of a subclass of UIView
#property (weak, nonatomic) IBOutlet MyView *myView1;
in
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(#"myView1: %#",self.myView1);
}
The size of the UIView is fully defined in the StoryBoard but comes out as zero when I run the code. Is this todo with auto layout or is the subview simply not defined yet? Am I doing something basically wrong here?
Just found one possible answer here. Running the methods
[self.view setNeedsLayout];
[self.view layoutIfNeeded];
forces a calculation of UIViews dimensions causing the expected values to appear in the log and also be known unto the UIView subclass itself.
I just printed the frame of a subview.
It was 0 in viewDidLoad.
It had a non-zero value inside the viewDidLayoutSubviews callback.
Inside the viewDidLoad you should only expect that the value of the viewcontroller's view to be non-zero. Inside viewDidLoad is actually where sometimes you add subviews and stuff. So it's not correct to expect the frame to get calculated in the very callback that you're settings constraints for it.
Basically what needs to be understood is that first a viewController is drawn, then as it goes through its life cycle events, its subviews will get laid out