UIScrollView as subview of UIView does not work with autolayout - ios

I created a customView (UIView) which contains a UIScrollView as its subview, and
objects I put on the scrollView did not show. To test it, I added a UIView to the scrollView and it didn't show neither(see code example below). Since the content I want to put on the scrollView is dynamic, I want to approach the problem by just using AutoLayout. I tried different ways and followed instructions online, none of it worked, all the similar questions were UIScrollView created inside of a UIViewController.
Here is my code, I'm using Masonry:
#interface CustomView : UIView
#end
#implementation CustomView
- (instancetype)init {
if (self = [super init]) {
self.scrollView = [[UIScrollView alloc]init];
self.scrollView.backgroundColor = [UIColor redColor];
[self addSubview:self.scrollView];
[self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self);
}];
//Here I add someView to the scrollView, and when I create an instance of the scrollView, the someView does not show.
self.someView = [[UIView alloc]init];
self.someView.backgroundColor = [UIColor blackColor];
[self.scrollView addSubview:self.someView];
[self.someView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.scrollView);
make.size.equalTo(CGSizeMake(40, 40));
}];
}
return self;
}
Here is how I create an instance of the CustomView
CustomView *customView = [[CustomView alloc]init];
[self.view addSubview: customView];
[customView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(64.0);
make.height.equalTo(40.0);
make.leading.trailing.equalTo(0);
}];
Is there anything I missed or did wrong that caused someView to not show?
Thank your for your help.

I solved the problem by: 1).Using scrollView directly instead of as a subView in a UIView (my customView). 2) make sure to add the right and bottom constraints of the scrollView.

Related

Copying UIButtons frame gives wrong position

I want to create another UIView / UIButton on an UIButton so I'm getting its' frame and use it in declaration of another UIView but It creates like below screenshot (with blue view). By the way, button is created on storyboard.
I'm using Xcode 9.1 and iOS 11.1
- (void)viewDidLoad {
UIView *picker = [[UIView alloc] initWithFrame:_mButton.frame];
picker.backgroundColor=[UIColor blueColor];
[self.view addSubview: picker];
}
Screenshot 1
Screnshot 2
I guess the proper frames are not calculated in viewDidLoad. You can try to make picker a property and adjust its frame in viewDidLayoutSubviews
#property (strong, nonatomic) UIView *picker;
- (void)viewDidLoad {
[super viewDidLoad];
self.picker = [[UIView alloc] initWithFrame:_mButton.frame];
self.picker.backgroundColor=[UIColor blueColor];
[self.view addSubview: self.picker];
}
- (void)viewDidLayoutSubviews {
self.picker.frame = _mButton.frame;
[super viewDidLayoutSubviews];
}
But off course using storyboard and autolayout would probably be the best sollution if you have this possibility.

How to keep the autolayout of xib then add this to a scrollView?

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:

UIScrollView is not scrolling in UiView

when I scroll on UIScrollView that not scrolling . Also I am setting scrollview's contentSize.
When I show one other subView and after when I show my view , it's scrolling.
You can see what is problem on this video . How can I solve this problem?
myViewController.h file
#import <UIKit/UIKit.h>
#interface myViewController : UIViewController
#property(nonatomic,retain)UIView * rightTabView;
#property(nonatomic,retain)UIView * leftTabView;
#property(nonatomic,retain)UIView * middleTabView;
#property(nonatomic,retain)UIScrollView *myscrollview;
#end
myViewController.m file
- (void)viewDidLoad
{
// Do any additional setup after loading the view from its nib.
[super viewDidLoad];
//create Tab views and addSubview
CGFloat barHeight=58;
self.rightTabView=[[UIView alloc] initWithFrame:CGRectMake(0, barHeight+55,self.view.bounds.size.width,self.view.bounds.size.height-barHeight-55)];
[self.view addSubview:self.rightTabView];
self.leftTabView=[[UIView alloc] initWithFrame:CGRectMake(0, barHeight+5,self.view.bounds.size.width,self.view.bounds.size.height-barHeight-55)];
self.leftTabView.backgroundColor=[UIColor redColor];
[self.view addSubview:self.leftTabView];
self.middleTabView=[[UIView alloc] initWithFrame:CGRectMake(0, barHeight+55,self.view.bounds.size.width,self.view.bounds.size.height-barHeight-55)];
[self.view addSubview:self.middleTabView];
//add Subview to leftTabView
myscrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(10, 10,300, 340)];
myscrollview.userInteractionEnabled=YES;
myscrollview.backgroundColor = [UIColor greenColor];
myscrollview.delegate = self;
[self.leftTabView addSubview:myscrollview];
myscrollview.contentSize = CGSizeMake(2000, 2000);
}
It looks like your middleTabView is obscuring your leftTabView (which is the superview of your scrollview). Either bring the leftTabView to the top of the view hierarchy:
[self.leftTabView.superview bringSubViewToFront:self.leftTabView];
or disable user interactions or visibility of your middleTabView (or add the leftTabView last).

Not able to add subview to my UIView

I have a UIView element on the screen. It is connected by an IBOutlet radioButtonGroupView. I am trying to add a subview to that element and it is not working. The subview is never added.
Here is my code:
-(id) initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
subView.backgroundColor = [UIColor yellowColor];
[self.radioButtonGroupView addSubview:subView];
return self;
}
You will probably be better off if you make this element a subclass of a UIViewController (instead of a UIView).
Then put the subview loading code inside viewDidLoad method.
- (void)viewDidLoad
{
[super viewDidLoad]; //not really needed but it doesn't hurt
UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
subView.backgroundColor = [UIColor yellowColor];
[self.radioButtonGroupView addSubview:subView];
}
EDIT:
You say that you don't get any frame information on self.radioButtonGroupView in your viewDidLoad. That is almost a certain sign that your IBOutlet is not properly connected to the element in InterfaceBuilder. You can always make a simple test:
- (void)viewDidLoad
{
[super viewDidLoad]; //not really needed but it doesn't hurt
NSLog (#"self.radioButtonGroupView: %#",self.radioButtonGroupView);
[self.radioButtonGroupView setHidden:YES];
}
If it is still shown - then it simply isn't properly connected. IB has it's ways. Simple
deletion of IBOutlet connection and reconnecting it again might do the trick. Also: it your radioButtonGroupView is not UIView but a subclass of UIView, make sure that it's header file is imported in your MyViewController.m file and class properly defined in .xib.

How to add multiple UIViews in a UIScrollView

I have a UIView that draws a pie chart and I would like to put 3 or maybe 4 charts in a UIScrollView programatically. How can I do that?
my h file is like this
#class PieChart;
#interface ViewController : UIViewController {
}
#property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
and m file
- (void)viewDidLoad
{
[super viewDidLoad];
[scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setCanCancelContentTouches:NO];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
PieChart *chart = [[PieChart alloc]initWithFrame:CGRectZero];
[chart setFrame:CGRectMake(100, 100, 200, 200)];
[scrollView addSubview:chart];
}
If you need to add subview to the view you need to use [view addSubview:subview]. This is common practice.
Read about frames and bounds (coordinate systems for view and sub views):
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaViewsGuide/Coordinates/Coordinates.html
As I understand you have a problem due to content size of scroll view. Read more here:
http://developer.apple.com/library/ios/#DOCUMENTATION/WindowsViews/Conceptual/UIScrollView_pg/Introduction/Introduction.html
Hope it helps.
You can just add it with:
[scrollView addSubview:singleView];
You add multiple subviews to a UIScrollView just like you add a single subview; with addSubview:. What problems are you running into?

Resources