I have a custom view with height 800 in XIB as seen on the picture :
this is the hierarchy :
this the code on my customView.m :
- (id)initWithFrame:(CGRect)frame
{
NSLog(#"initWithFrame");
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
NSLog(#"initWithCoder");
self = [super initWithCoder:aDecoder];
if(self) {
[self setup];
}
return self;
}
- (void)setup {
[[NSBundle mainBundle] loadNibNamed:#"customView" owner:self options:nil];
[self addSubview:self.view];
self.view.frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, CGRectGetHeight(self.view.frame));
//[self.myscrollview setContentSize:CGSizeMake([[UIScreen mainScreen] bounds].size.width, 2000)];
NSLog(#"contentSize Height :%f", self.myscrollview.contentSize.height);
NSLog(#"contentView Height :%f", self.contentView.frame.size.height);
}
and addSubview the customView on my viewController view :
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
customView *myView = [[customView alloc] init];
[self.view addSubview:myView];
}
it is loaded, but the scrollView is not scrolling. Then I check the customView and contentView height it returns 800 but the scrollView contentSize height it returns 0. so I tried to set the contentSize to 2000 but it still not scrolling. the thing that made me confused is that why after I set the scrollView trailing, leading, bottom and top 0 to superView it is not following the contentView height, instead return 0. How am i supposed to set the contentSize then?
nb: the contentView has height and width equal to superView and the priority set to 250
github : project example
It's because you didn't set frame for myView. Replace your viewDidLoad method with below code and it will work as expected
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
customView *customV = [[customView alloc] initWithFrame:self.wantToShowHereView.bounds];
[self.wantToShowHereView addSubview:customV];
}
Related
I have an autolayout xib with constraints added in my application.
Then I add my xib view to a scrollView lateral like a pageController.
But when I add this to the scrollView I get a lot of error with constraints and the outlets isn't showed properly. "[LayoutConstraints] Unable to simultaneously satisfy constraints."
This is my xib class code:
#implementation InvitView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
nibname=#"Invit";
[self addSubview:
[[[NSBundle mainBundle] loadNibNamed:nibname owner:self options:nil] objectAtIndex:0]];
}
return self;
}
#end
And I add my xib to the scrollView with this code:
- (void)viewDidLoad {
[super viewDidLoad];
float width = self.scroll.frame.size.width;
float height = self.scroll.frame.size.height;
int count = 0;
InvitView *myView = [[InvitView alloc] initWithFrame:CGRectMake(self.scroll.frame.origin.x, self.scroll.frame.origin.y, self.scroll.frame.size.width, self.scroll.frame.size.height)];
[myView setFrame:CGRectMake(width*count++, 0, width, height)];
[myView setNeedsUpdateConstraints];
[self.scroll addSubview:myView];
}
I'm trying set translatesAutoresizingMaskIntoConstraints = NO; but with this code I lost all constraints, and I want keep this then add to the scrollView.
How can I keep my initial constraints in the xib?
EDIT
On the one hand, I have this constraints to my UIScrollView in my viewwController:
On the another hand I have this in my xib:
And the exactly log is:
I have a tableview with 2 rows. In the first row I have an UIView and I'm gonna add a custom view on it. I made the custom view as a XIB and I'm adding it like following,
Layout6View *lookLayout6 = [[Layout6View alloc] initWithFrame:CGRectMake(0, 0,cell.mainView.frame.size.width, cell.mainView.frame.size.height)];
[cell.mainView addSubview:lookLayout6];
and my Layout6View looks like,
#implementation KCTLLookLayout6View
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self = [[[NSBundle mainBundle] loadNibNamed:#"Layout6"
owner:self
options:nil] lastObject];
self.frame = frame;
}
return self;
}
-(void)layoutSubviews{
[super layoutSubviews];
for (UIView *view in self.subviews) {
if (view == self.imageTopRightCorner) {
view.frame = CGRectOffset(CGRectMake(0, 0, /*(self.frame.size.width/2)-20, (self.frame.size.height/3)*/30,30), 0, 0);
}
}
}
#end
When I'm running this, the custom view didn't resized. It's spread all over the table view(not only inside the cell).
Is there a way to fix this?
Try:
[cell.contentView addSubview:lookLayout6];
Change the Size property of xib view. By default it is Inferred, make it Freeform and check.
I have a subclass of UIScrollView and I am trying to add it to my view. For some reason when I add it in the viewDidLoad it works as expected. But when I try adding it in the viewWillAppear then it seems to change the contentSize (although when I print the contentSize it is 905 in either case)
This works as expected:
- (void)viewDidLoad
{
[super viewDidLoad];
diptic = [[DipticView alloc] initWithFrame:self.view.frame];
diptic.item = self.item;
[self.view addSubview:diptic];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
}
This cuts off the scrollView (both views are scrolled down all the way.)
- (void)viewDidLoad
{
[super viewDidLoad];
diptic = [[DipticView alloc] initWithFrame:self.view.frame];
diptic.item = self.item;
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.view addSubview:diptic];
}
Any ideas on why this might be happening?
EDIT:
I think the frame is being reset somewhere because if I do :
diptic = [[DipticView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
I get the same results...
firstly I want to say that I am newbie at iOS development. I have a problem with showing a view from xib file in ScrollView.
This MyView is only green background with one label. Here is a code from its controller:
#implementation MyView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//init code
}
return self;
}
- (id) initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder]) {
[self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
}
return self;
}
#end
And here is a code from main controller where is the ScrollView:
- (void)viewDidLoad
{
[super viewDidLoad];
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 67, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
scroll.frame = CGRectMake(0, 20, 320, 350);
scroll.contentSize = CGSizeMake(320, 350);
scroll.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
scroll.bounces = YES;
scroll.bouncesZoom = YES;
scroll.scrollEnabled = YES;
scroll.minimumZoomScale = 0.5;
scroll.maximumZoomScale = 5.0;
scroll.delegate = self;
scroll.pagingEnabled = YES;
NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:#"MyView" owner:self options:nil];
UIView *myView = [nibContents objectAtIndex:0];
myView.frame = CGRectMake(0,20,300,300);
[scroll addSubview:myView];
}
Only thing I want to do is that I want to see MyView (green background with one label) inside the ScrollView. If I run this app, scrollview is empty, nothing is shown inside.
Probably it is a stupid question but as I said, I am newbie at this. I have a PageControll app from Apple but for me this is quite complicated code in my iOS beginning. Of course I was googling... Please tell me where is a problem. Thank you
Where do you define scroll? Is it a property of your viewcontroller?
I think the problem here is that you don't add scroll view to your viewcontroller's view with
[self.view addSubview:scroll]
Everything seems to right. Just retain the view in your class that you are receiving from the following line and add that as a subview to your scrollview.
self.myView = [nibContents objectAtIndex:0];
Best Regards.
I'm trying to subclass a UIView that contains a UIScrollView inside of it. This is the code I'm using:
UIViewSubclass.h
#property (nonatomic, retain) UIScrollView *scroll;
UIViewSubclass.m
#synthesize itemScrollView;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setUpScrollViewWithIdentifier:#"id"];
}
return self;
}
-(void)setUpScrollViewWithIdentifier:(NSString*)scrollViewID {
NSLog(#"Setting up Scroll View with the id of: %#",scrollViewID); //This is working
scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 90)];
//Subview of the scroll view
UIView *test = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
test.backgroundColor = [UIColor blueColor];
[scroll addSubview:test];
[self addSubview:scroll]; // Adds the scrollview to the UIView
NSLog(#"Subviews: %#",self.subviews);
}
ViewController.h
#import "UIViewSubclass.h"
IBOutlet UIViewSubclass *menuView;
View Controller.m
- (void)viewDidLoad
{
menuView = [[UIViewSubclass alloc]init];
[super viewDidLoad];
// Do any additional setup after loading the view.
}
I also linked the view up in the storyboard and set the view's class to UIViewSubclass but for somereason the scroll view won't appear. But when i check this NSLog:
NSLog(#"Subviews: %#",self.subviews);
It gives me this in the debugger:
Subviews: (
"<UIScrollView: 0x1d5a20e0; frame = (0 0; 320 90); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x1d5a2420>; layer = <CALayer: 0x1d5a1c60>; contentOffset: {0, 0}>"
)
So what am I doing wrong?
scroll.contentSize = fooSize;
might be what you missing.