I'm trying to modify Apple's PhotoScroller example to make the scrollview that is created into a subview instead of it being a view that takes up the entire screen. Any ideas on how this can be accomplished?
- (void)loadView
{
// Step 1: make the outer paging scroll view
CGRect pagingScrollViewFrame = [self frameForPagingScrollView];
pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame];
pagingScrollView.pagingEnabled = YES;
pagingScrollView.backgroundColor = [UIColor blackColor];
pagingScrollView.showsVerticalScrollIndicator = NO;
pagingScrollView.showsHorizontalScrollIndicator = NO;
pagingScrollView.contentSize = [self contentSizeForPagingScrollView];
pagingScrollView.delegate = self;
// When I do this it fails
[self.view addSubview:pagingScrollView];
// Step 2: prepare to tile content
recycledPages = [[NSMutableSet alloc] init];
visiblePages = [[NSMutableSet alloc] init];
[self tilePages];
}
You just need to modify the frame of the scrollview to be positioned and sized how you want:
This is the line in the view controller that sets it up in the example
pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame];
As an example here is a sample frame with some hardcoded values:
CGRect scrollFrame = CGRectMake(100,100,100,100);
pagingScrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
So, I found out that I was able to add the scrollView as a subview by changing the method from loadView to viewDidLoad.
I have no clue why that works, but it does. I'd love to know why that's the case however...
Related
I'm trying to add an imageView in the ScrollView but I always get the ScrollView empty. Please where would be my issue? While I have added the delegate method <UIScrollViewDelegate> also made the connection in the storyboard.
My code:
- (void) viewDidLoad {
[super viewDidLoad];
[self slider];
}
-(void) slider {
_scrlView = [[UIScrollView alloc]init];
_scrlView.backgroundColor = [UIColor blackColor];
_scrlView.pagingEnabled = YES;
_scrlView.delegate = self;
_scrlView.showsHorizontalScrollIndicator = false;
_scrlView.layer.cornerRadius = 2;
_scrlView.contentSize = CGSizeMake(_scrlView.frame.size.width * 3, _scrlView.frame.size.height);
_img1 = [[UIImageView alloc] initWithFrame:CGRectMake((_scrlView.frame.size.width*0),0,_scrlView.frame.size.width,_scrlView.frame.size.height)];
_img2 = [[UIImageView alloc] initWithFrame:CGRectMake((_scrlView.frame.size.width*1),0,_scrlView.frame.size.width,_scrlView.frame.size.height)];
_img3 = [[UIImageView alloc] initWithFrame:CGRectMake((_scrlView.frame.size.width*2),0,_scrlView.frame.size.width,_scrlView.frame.size.height)];
_img1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"1234.jpg"]];
_img2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"1234.jpg"]];
_img3 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"1234.jpg"]];
[_scrlView addSubview:_img1];
[_scrlView addSubview:_img2];
[_scrlView addSubview:_img3];
}
First, you don't add the scrollView to any view, so it won't be presented on screen.
Second, this:
_scrlView = [[UIScrollView alloc]init];
creates a scrollView with CGRectZero which is the same like writing:
_scrlView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,0,0)];
This means the contentSize is also CGRectZero.
Edit: If you don't want to override the creation of the scrollView (because you have already set it in Interface Builder), remove the line _scrlView = [[UIScrollView alloc]init];.
But if you are using Auto Layout in the storyboard you should call [self slider] in viewDidLayoutSubviews.
You need to set frame first, for getting its width and height,
- (void)viewDidLoad {
[super viewDidLoad];
[self slider];
}
-(void)slider{
_scrlView = [[UIScrollView alloc] initWithFrame:CGRectMake(YOUR_X, YOUR_X, YOUR_WIDTH, YOUR_HIEGHT)];
// do this if you have not added Scrollview in storyboard, if Added, then no need to invoke, init by line [[UIScrollView alloc] init] ;
...
...
[self.view addSubview:_scrlView];
}
I would suggest one more thing, to call your slider method from viewDidAppear method of UIView
Enjoy Coding !!
You need to allocate the ScrollView if have not used a storyboard. Also in that case, specify a frame:
_scrlView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)];
You are reallocating the imageviews, try to set image after allocating it once.
Another thing : Dont init scrollview again.
- (void)viewDidLoad {
[super viewDidLoad];
[self slider];
}
-(void)slider{
_scrlView.backgroundColor = [UIColor blackColor];
_scrlView.pagingEnabled = YES;
_scrlView.delegate = self;
_scrlView.showsHorizontalScrollIndicator = false;
_scrlView.layer.cornerRadius = 2;
_scrlView.contentSize = CGSizeMake(_scrlView.frame.size.width * 3, _scrlView.frame.size.height);
_img1 = [[UIImageView alloc] initWithFrame:CGRectMake((_scrlView.frame.size.width*0),0,_scrlView.frame.size.width,_scrlView.frame.size.height)];
_img2 = [[UIImageView alloc] initWithFrame:CGRectMake((_scrlView.frame.size.width*1),0,_scrlView.frame.size.width,_scrlView.frame.size.height)];
_img3 = [[UIImageView alloc] initWithFrame:CGRectMake((_scrlView.frame.size.width*2),0,_scrlView.frame.size.width,_scrlView.frame.size.height)];
[_img1 setImage:[UIImage imageNamed:#"1234.jpg"]];
[_img2 setImage:[UIImage imageNamed:#"1234.jpg"]];
[_img3 setImage:[UIImage imageNamed:#"1234.jpg"]];
[_scrlView addSubview:_img1];
[_scrlView addSubview:_img2];
[_scrlView addSubview:_img3];
}
My reply would be
if you are not using Storyboard or Interface builder , also autolayout is unchecked, set the frame of uiscrollview after its allocation and use alloc init only one time for one image. Add image on scrollview and scrollview on superview
if you are not using Storyboard or Interface builder and autolayout is checked, then put proper constraints on scrollview and imageviews. Read this
https://developer.apple.com/library/ios/technotes/tn2154/_index.html
I'm using the JASidePanels. I have set my project and storyboard to show in landscape orientation only. The result shown as below. It seems like the view are read as portrait by JASidePanels. However when i turn the device into to the other side, the panel orientation will be corrected. Please advise how to fix this issue.
Thanks.
You can add the following line in viewDidAppear() :
[self _layoutSideContainers:YES duration:0];
Try replacing the -(void)_layoutSidePanels method with this:
- (void)_layoutSidePanels {
if (self.rightPanel.isViewLoaded) {
....
}
if (self.leftPanel.isViewLoaded) {
CGRect frame = self.leftPanelContainer.bounds;
frame.size.width = floorf(self.view.bounds.size.width * self.rightGapPercentage);
if (self.shouldResizeLeftPanel) {
frame.size.width = floorf(self.view.bounds.size.width * self.rightGapPercentage);
}
self.leftPanel.view.frame = CGRectMake(0, 0, self.view.bounds.size.width * self.rightGapPercentage, self.view.bounds.size.height);
}
}
Please give me a feedback, I'm not sure if this is the problem
I think i found the solution. I replace the following code in viewDidLoad() to viewDidAppear(). The problem is after the view did appear, then it will get the correct view width.
self.centerPanelContainer = [[UIView alloc] initWithFrame:self.view.bounds];
_centerPanelRestingFrame = self.centerPanelContainer.frame;
_centerPanelHidden = NO;
self.leftPanelContainer = [[UIView alloc] initWithFrame:self.view.bounds];
NSLog(#"%f",self.leftPanelContainer.frame.size.width);
self.leftPanelContainer.hidden = YES;
self.rightPanelContainer = [[UIView alloc] initWithFrame:self.view.bounds];
self.rightPanelContainer.hidden = YES;
[self _configureContainers];
[self.view addSubview:self.centerPanelContainer];
[self.view addSubview:self.leftPanelContainer];
[self.view addSubview:self.rightPanelContainer];
self.state = JASidePanelCenterVisible;
[self _swapCenter:nil previousState:0 with:_centerPanel];
[self.view bringSubviewToFront:self.centerPanelContainer];
Is it possible to use UIScrollViewController to scroll or "page" two or more views coming from UIViewControllers?
Example (in viewDidLoad)
self.a1 = [[CustomViewController1 alloc] init];
self.a2 = [[CustomViewController2 alloc] init];
//Scroller
self.scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,1000,400
400)];
[self.scroller addSubview:self.a1.view];
[self.scroller addSubview:self.a2.view];
[self.view addSubview:self.scroller];
But I can see in the scroller only the view of controller a2.
You need to set the frames of the views so they they sit one below another. Currently they are both there, just on top of one another.
Try putting in
self.a2.view.frame = CGRectMake(0, 480, 320, 480);
You have to modify the frames aswell. Now a1 and a2 have the same, and they are on top of eachother, so the a2 is visible, because you added the later.
Try
self.a2.view.frame = CGRectOffset(self.a2.view.frame, self.a1.view.frame.size.width, 0);
The solution is to set the frame coordinates of the subviews inside the scroller:
CGRect frame = CGRectMake(0, 0, 1000, 600);
self.a1 = [[CustomViewController1 alloc] init];
self.a1.view.frame = frame;
self.a2 = [[CustomViewController2 alloc] init];
frame.origin.x = frame.size.width +10;
self.a2.view.frame = frame;
[self.scroller addSubview:self.a1.view];
[self.scroller addSubview:self.a2.view];
[self.view addSubview:self.scroller];
You'll need to add the custom view controllers as child view controllers. See Apple's documentation for more information on doing that correctly.
Disclaimer: I'm something of an iOS n00b. I have a navigation-based app -- i.e., in my AppDelegate, I create the nav frame:
self.navigation_controller = [[UINavigationController alloc] initWithRootViewController:home_view_controller];
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
self.window.rootViewController = navigation_controller;
self.window.makeKeyAndVisible;
Inside the frame of my home view, I want a search bar, then the scrollable table view. So, I wrote this:
- viewDidLoad{
(HomeView*)home_view = [[HomeView alloc] init];
self.view = home_view
self.view.backgroundColor = [UIColor whiteColor]
(UITableView*)self.table_view = [UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
self.table_view.bounds.origin.y += 44;
self.table_view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.table_view.dataSource = self;
self.table_view.delegate = self;
[self.view addSubview:self.table_view];
search_frame = self.view.bounds;
search_frame.origin.y = 48.0;
search_frame.size.height = 48.0;
self.search_bar = [[UISearchBar alloc] initWithFrame:search_frame)];
[self.view addSubview:self.search_bar];
}
The table view displays fine, but occupies all the space below the navigation bar. I want to have the navigation bar, then immediately below it the search bar, followed by the table view. At the point of viewDidLoad, the UITableView does not yet have a non-zero bounding rect and I expect that's part of the problem. Additionally, I've specified UIViewAutoresizingFlexibleHeight in my autoresizingMask when I really want it glued against the search bar -- again, I believe this may be part of the problem.
What have I misunderstood here?
Thanks
Steve,
Try following changes:
1) Set tableview's frame using:
CGRect targetRect = CGRectMake(0, 48, self.view.frame.size.width, self.view.frame.size.heigth);
(UITableView*)self.table_view = [UITableView alloc] initWithFrame:targetRect style:UITableViewStylePlain];
2) Add following autosize mask also:
// This value will anchor your table view with top margin
UIViewAutoresizingFlexibleBottomMargin
3) Set searchbar's frame to following rect frame:
CGRecttMake(0, 0, self.view.frame.size.width, 48);
What I would do, is just add the UISearchBar as the first UITableView header.
(method: tableView:viewForHeaderInSection:)
Check this blog post:
http://jainmarket.blogspot.com/2009/08/add-searchbar-as-uitableviews-header.html
So, as the title says, I have an hdmi out on the iPad and an observer registered for screen connections, upon connection the user chooses the res and a view is outputted.
However, if I load a view from a nib, or even from a programatic view controller, the ipad shows a landscape view in portrait (yes, both situations are set to landscape).
I.e.
ExternalViewController *ex = [[ExternalViewController alloc] init];
[externalWindow setRootViewController:ex];
does this:
If I create the view itself programatically. like so:
UIView *test = [[UIView alloc] initWithFrame:[externalScreen applicationFrame]];
[test setBackgroundColor:[UIColor whiteColor]];
UILabel *msgLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 40, 100, 30)];
msgLabel.text = #"External!";
[test addSubview:msgLabel];
It runs like some form of magical dream:
However I want the viewcontroller to load (and work!) so, StackOverflow, I ask you. has anyone come across this before?
EDIT: It does go without saying that common sensical answers do not get a bounty, I am after a fix, not a workaround. With my limited brain, all I can think to do is create a method that creates a view based on it's inputs and adds that as a subview of the external monitor, it is clear that this is a hack solution so a fix is appreciated! Thanks!
EDIT:
-(id)initWithFrame:(CGRect)_rect
{
rect = _rect;
if (self = [super init])
{
externalView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"105.png"]];
externalView.alpha = 0.0;
[externalView setFrame:rect];
[externalView setBackgroundColor:[UIColor yellowColor]];
self.view = [[UIView alloc] initWithFrame:rect];
self.view.backgroundColor = [UIColor whiteColor];
return self;
}
}
- (void)loadView
{
self.view = [[UIView alloc] initWithFrame:rect];
self.view.backgroundColor = [UIColor blackColor];
[self.view addSubview:externalView];
}
As requested, this is how I am loading the viewcontroller, initialising with the size of the external screen. Thanks
Just return NO in - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation in your UIViewController subclass.
// ugly, don't use this in real code
if ([UIScreen screens].count == 1) return; // just one screen, eww.
// getting the secondary screen
UIScreen *screen = [[UIScreen screens] objectAtIndex:1];
__block UIScreenMode *highestWidthMode = NULL;
[screen.availableModes enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
UIScreenMode *currentModeInLoop = obj;
if (!highestWidthMode || currentModeInLoop.size.width > highestWidthMode.size.width)
highestWidthMode = currentModeInLoop;
}];
// setting to the highest resolution available
screen.currentMode = highestWidthMode;
NSLog(#"screen.currentMode = %#", screen.currentMode);
screen.overscanCompensation = UIScreenOverscanCompensationScale;
// initializing screen
secondWindow = [[UIWindow alloc] initWithFrame:[screen bounds]];
[secondWindow setScreen:screen];
// other view is a UIViewController, just remember to return NO in - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation or the iOS will rotate the frame.
UIViewController *vc = [[OtherView alloc] initWithNibName:#"OtherView" bundle:nil];
secondWindow.rootViewController = vc;
[secondWindow makeKeyAndVisible];