I've created a UIScrollView programatically, and it is not scrolling:
CGRect prevFrame;
BOOL isFirst = TRUE;
UIScrollView *view_scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
view_scroll.contentSize = CGSizeMake(650,224*[_cases count]+25*[_cases count]);
view_scroll.showsVerticalScrollIndicator = YES;
view_scroll.scrollEnabled = YES;
NSLog(#"%f",view_scroll.contentSize.height);
[self.view addSubview:view_scroll];
for(SLSCase* patientCase in _cases){
SLSCaseCard* caseCard = [[[NSBundle mainBundle] loadNibNamed:#"SLSCaseCard" owner:self options:nil] objectAtIndex:0];
if(isFirst){
caseCard.frame = CGRectMake(25,25, caseCard.frame.size.width, caseCard.frame.size.height);
isFirst = FALSE;
}
else{
caseCard.frame = CGRectMake(25, prevFrame.origin.y+caseCard.frame.size.height+25, caseCard.frame.size.width, caseCard.frame.size.height);
}
prevFrame = caseCard.frame;
[view_scroll addSubview:caseCard];
}
As you can see I've set the contentSize and scrollEnabled. It's still not scrolling.
Due to a scrollView feature in auto layout, the contentSize is being redefined. To solve this problem, you could disable auto layout or set the content size in viewDidLayoutSubviews()
-(void)viewWillLayoutSubviews {
[super viewDidLayoutSubviews];
view_scroll.contentSize = CGSizeMake(650,224*[_cases count]+25*[_cases count]);
}
Related
I have UIViewcontroler which occupied with two component one UITableView of the size Width: 120 and Height:603 of UIViewcontroller and another one UIView (detailView) related screen. (size Width: 255 & Height: 603 )
Now I created on custom UIView (customView) now I want to added it on detailView, which is not fit to UIView
detailView Frame is half of uiviewcontroller view size.
the custom view frame is normal i.e width 375 & height 667
-(void)loadView {
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:#"CustomView" owner:self options:nil];
customView = [nibObjects objectAtIndex:0];
customView.frame = detailView.frame;
customView.layer.shadowColor = [UIColor blackColor].CGColor;
customView.layer.shadowRadius = 1.0f;
customView.layer.shadowOpacity = 1;
customView.layer.shadowOffset = CGSizeZero;
customView.layer.masksToBounds = YES;
[detailView addObject:customView];
}
The CustomView is not auto adjusted on the detailView UIView Screen.
Your advice will be useful for understand to fix frame
#Thanks in advance
The problem is you are setting the detailView's frame to your customView and adding the customView into your detailView.
to set the frame of your custom view please use below code:
customView.frame = (CGRect){
.origin = CGPointZero,
.size = detailView.frame.size
};
Hope fully this will solve your problem.
Your loadView Method is written like below
-(void)loadView {
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:#"CustomView" owner:self options:nil];
customView = [nibObjects objectAtIndex:0];
customView.frame = CGRectMake(0, 0, detailView.frame.size.width, detailView.frame.size.height);
customView.layer.shadowColor = [UIColor blackColor].CGColor;
customView.layer.shadowRadius = 1.0f;
customView.layer.shadowOpacity = 1;
customView.layer.shadowOffset = CGSizeZero;
customView.layer.masksToBounds = YES;
[detailView addSubview:customView];
}
I want to zoom in/out UIScrollView that contains a UIImageView. With my code below I am only able to scroll the scroll view contents but cannot zoom it.
My view hierarchy looks like this:
- (UIView *) containerView
-- (UIView *) contentView
--- (UIScrollView *) scrollView
---- (UIImageView *) self.imageView
Code:
UIView *previousContentView = nil;
for (NSInteger i = 0; i < 2; i++) {
contentView = [self addRandomColoredView];
[containerView addSubview:contentView];
[containerView.topAnchor constraintEqualToAnchor:contentView.topAnchor].active = true;
[containerView.bottomAnchor constraintEqualToAnchor:contentView.bottomAnchor].active = true;
scrollView = [self addRandomScrollView];
[contentView addSubview:scrollView];
self.imageView = [[UIImageView alloc] init];
[self.imageView setImage:[imagesArray objectAtIndex:i]];
[self.imageView setTranslatesAutoresizingMaskIntoConstraints:false];
[scrollView addSubview:self.imageView];
if (previousContentView) {
[VerticalSeparatorView addSeparatorBetweenView:previousContentView secondView:contentView];
NSLayoutConstraint *width = [contentView.widthAnchor constraintEqualToAnchor:previousContentView.widthAnchor];
width.priority = 250;
width.active = true;
} else {
[containerView.leadingAnchor constraintEqualToAnchor:contentView.leadingAnchor].active = true;
}
[contentView.topAnchor constraintEqualToAnchor:scrollView.topAnchor].active = true;
[contentView.bottomAnchor constraintEqualToAnchor:scrollView.bottomAnchor].active = true;
[contentView.leadingAnchor constraintEqualToAnchor:scrollView.leadingAnchor].active = true;
[contentView.trailingAnchor constraintEqualToAnchor:scrollView.trailingAnchor].active = true;
[scrollView.topAnchor constraintEqualToAnchor:self.imageView.topAnchor].active = true;
[scrollView.bottomAnchor constraintEqualToAnchor:self.imageView.bottomAnchor].active = true;
[scrollView.leadingAnchor constraintEqualToAnchor:self.imageView.leadingAnchor].active = true;
[scrollView.trailingAnchor constraintEqualToAnchor:self.imageView.trailingAnchor].active = true;
previousContentView = contentView;
}
[containerView.trailingAnchor constraintEqualToAnchor:previousContentView.trailingAnchor].active = true;
- (UIView *)addRandomColoredView
{
UIView *someView = [[UIView alloc] init];
someView.translatesAutoresizingMaskIntoConstraints = false;
[someView setBackgroundColor:[UIColor blackColor]];
return someView;
}
-(UIScrollView *)addRandomScrollView
{
UIScrollView *scrollView = [[UIScrollView alloc] init];
[scrollView setDelegate:self];
[scrollView setTranslatesAutoresizingMaskIntoConstraints:false];
[scrollView setBackgroundColor:[UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]];
[scrollView setMaximumZoomScale:2.5f];
[scrollView setMinimumZoomScale:0.5f];
[scrollView setZoomScale:scrollView.minimumZoomScale];
return scrollView;
}
#pragma mark - UIScrollViewDelegate
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}
I believe the scrollView is getting its contentSize because it is showing the image and I can scroll. Why can't I zoom in or zoom out?
You have to specify a delegate for your scroll view, and then implement viewForZoomingInScrollView to tell it to zoom the image view:
- (UIView *)addScrollViewWithImageView {
UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.translatesAutoresizingMaskIntoConstraints = false;
scrollView.maximumZoomScale = 200.0;
scrollView.minimumZoomScale = 0.1;
scrollView.delegate = self;
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"test.png"]];
imageView.translatesAutoresizingMaskIntoConstraints = false;
[self.view addSubview:scrollView];
[scrollView addSubview:imageView];
[NSLayoutConstraint activateConstraints:#[
[imageView.topAnchor constraintEqualToAnchor:scrollView.topAnchor],
[imageView.leftAnchor constraintEqualToAnchor:scrollView.leftAnchor],
[imageView.rightAnchor constraintEqualToAnchor:scrollView.rightAnchor],
[imageView.bottomAnchor constraintEqualToAnchor:scrollView.bottomAnchor]
]];
return scrollView;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return scrollView.subviews.firstObject;
}
And then to populate these zoomable scroll views with image views, your viewDidLoad might look like:
- (void)viewDidLoad {
[super viewDidLoad];
UIView *previousContentView = nil;
for (NSInteger i = 0; i < 3; i++) {
UIView *contentView = [self addScrollViewWithImageView];
[self.view.leadingAnchor constraintEqualToAnchor:contentView.leadingAnchor].active = true;
[self.view.trailingAnchor constraintEqualToAnchor:contentView.trailingAnchor].active = true;
if (previousContentView) {
[HorizontalSeparatorView addSeparatorBetweenView:previousContentView secondView:contentView];
NSLayoutConstraint *height = [contentView.heightAnchor constraintEqualToAnchor:previousContentView.heightAnchor];
height.priority = 250;
height.active = true;
} else {
[self.view.topAnchor constraintEqualToAnchor:contentView.topAnchor].active = true;
}
previousContentView = contentView;
}
[self.view.bottomAnchor constraintEqualToAnchor:previousContentView.bottomAnchor].active = true;
}
Try MWPhotoBrowser. It has built in scroll and zoom functionality. You will get more than you need. Good Luck.
I am creating a customview which is a subclass of UIScrollView. I am adding UIButtons inside UIScrollView dynamically along with images and actions. But scrollview is not scrolling horizontally with buttons. I know this topic is discussed many times on SO. I tried all posts posted on SO, but not getting expected result. Any suggestion about where i am going wrong will be helpful.
MyCustomView.h
#interface MyCustomView : UIScrollView
#property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
+ (id)customView;
#end
MyCustomView.m
#implementation MyCustomView
#synthesize scrollView;
+ (id)customView
{
MyCustomView *customView = [[[NSBundle mainBundle] loadNibNamed:#"MyCustomView" owner:nil options:nil] lastObject];
if ([customView isKindOfClass:[MyCustomView class]])
return customView;
else
return nil;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (void)drawRect:(CGRect)rect
{
[self setupHorizontalScrollView];
}
- (void)setupHorizontalScrollView
{
//scrollView.delegate = self;
[self.scrollView setBackgroundColor:[UIColor blueColor]];
[scrollView setCanCancelContentTouches:YES];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = NO;
scrollView.scrollEnabled = YES;
//scrollView.pagingEnabled = YES;
CGFloat cx = 0;
NSArray *buttons = #[#{#"Tag":#1,#"Image":[UIImage imageNamed:#"borderImage1.png"]},
#{#"Tag":#2,#"Image":[UIImage imageNamed:#"borderImage2.png"]},
#{#"Tag":#3,#"Image":[UIImage imageNamed:#"borderImage3.png"]},
#{#"Tag":#4,#"Image":[UIImage imageNamed:#"borderImage4.png"]},
#{#"Tag":#1,#"Image":[UIImage imageNamed:#"borderImage1.png"]},
#{#"Tag":#2,#"Image":[UIImage imageNamed:#"borderImage2.png"]},
#{#"Tag":#3,#"Image":[UIImage imageNamed:#"borderImage3.png"]},
#{#"Tag":#4,#"Image":[UIImage imageNamed:#"borderImage4.png"]},
#{#"Tag":#1,#"Image":[UIImage imageNamed:#"borderImage1.png"]}
];
// CGRect frame = CGRectMake(0.0f, 0.0f, 50.0f, 30.0f);
for (NSDictionary *dict in buttons)
{
UIButton *button =[UIButton buttonWithType:UIButtonTypeRoundedRect];
CGRect rect = button.frame;
rect.size.height = 40;
rect.size.width = 40;
rect.origin.x = cx;
rect.origin.y = 0;
button.frame = rect;
button.tag = [dict[#"Tag"] integerValue];
[button setImage:dict[#"Image"]
forState:UIControlStateNormal];
// [button addTarget:self action:#selector(buttonAction:)
// forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:button];
//frame.origin.x+=frame.size.width+20.0f;
cx += button.frame.size.width+5;
}
[scrollView setContentSize:CGSizeMake(cx, [scrollView bounds].size.height)];
}
#end
I went through below SO links, but didn't get solution.
LINK1 LINK2LINK3 LINK4
To be able to scroll you need to keep a view over scrollview that goes out of the bound of the screen. So drag a view and enlarge it beyong the device bounds so that it would be able to hold your subviews.
Its better to use UICollectionView for this kind of a problem. It is easier to implement and also will help you do all necessary manipulations.
Your MyCustomView is a subClass of UIScrollView. So you don't need to add UIScrollView property to your class. Delete scrollView property and replace all self.scrollView with self
I have a UIScrollView (similar to that seen showing the featured items on the App Store) which has 3 views, with xibs, loaded into it. Everything works find except I can get the UIButtons which I have on the xibs to fire. I guess I am missing something obvious just can't see it. I've tried various answers on here and other places to no avail.
Thanks in advance
Code from viewDidLoad on ViewController containing scroll view
LSCWInfoView *view1 = [[[NSBundle mainBundle]
loadNibNamed:#"LSCW-InfoView"
owner:self options:nil]
firstObject];
CGRect frame;
frame.origin.x = 0;
frame.origin.y = 0;
frame.size = self.activityPageControl.frame.size;
[view1 setFrame:frame];
[self.activityScrollView addSubview:view1];
ScrambledInfoView *view2 = [[[NSBundle mainBundle]
loadNibNamed:#"Scrambled-InfoView"
owner:self options:nil]
firstObject];
CGRect frame2;
frame2.origin.x = 320;
frame2.origin.y = 0;
frame2.size = self.activityPageControl.frame.size;
[view2 setFrame:frame2];
[self.activityScrollView addSubview:view2];
TestInfoView *view3 = [[[NSBundle mainBundle]
loadNibNamed:#"Test-InfoView"
owner:self options:nil]
firstObject];
CGRect frame3;
frame3.origin.x = 640;
frame3.origin.y = 0;
frame3.size = self.activityPageControl.frame.size;
[view3 setFrame:frame3];
[self.activityScrollView addSubview:view3];
_activityScrollView.contentSize = CGSizeMake(_activityScrollView.frame.size.width * 3, _activityScrollView.frame.size.height);
EDIT - Code for one of the views loaded into the scroll view (other 2 are the same)
#import "LSCWInfoView.h"
#implementation LSCWInfoView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
- (IBAction)lscwPlayButtonPressed:(id)sender
{
NSLog(#"YEP");
}
#end
Screenshots
This was fixed by setting the frame size of the view before it is added to the scrollview
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.