Objective C addSubview elements are not clickable - objective-c

I Have a horizontal UIScrollView. I want to do a magazine app.
I create a dynamic UIview as this:
UIView *one=[[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width*4, 0, 0, 0)];
and my view:
pageOneViewController *pageOne=[[pageOneViewController alloc]init];
and then:
[one addSubview:pageOne.view];
[self.scroll addSubview:one];
with these codes, the elements that in pageOneViewController, can not clickable.
if I add "pageOneViewController.view" directly to the self.scroll they can.
how can I make clickable with first situation?

Here in your code
UIView *one=[[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width*4, 0, 0, 0)];
Here width and height are 0, try to set background color and non zero values for height and width for view and check your view in scrollView.
pageOneViewController *pageOne=[[pageOneViewController alloc]init];
// Y value as per your need, I have set 0.
UIView *one=[[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width*4, 0, pageOne.view.frame.size.width, pageOne.view.frame.size.height)];
[one addSubview:pageOne.view];
[self.scroll addSubview:one];

You can't add root view from view controller to other views.
Use UIPageViewController for creating viewcontrollers as pages.

Related

How to change the Title View of the navigation bar into some Custom View and fill the original size

I want to change the titleView of navigationBar with a regular text field. Then I want to set the textField size to fill the old "normal" titleView.
I can't seem to be able to do this in storyBoard. Dragging a text Field to the place where the navigation title View is doesn't work.
So I added stuff at
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
PO(self.navigationItem.titleView);
CGRect theFrame= self.navigationItem.titleView.frame;
self.navigationItem.titleView=self.searchBar;
//self.searchBar.frame = theFrame;
while (false);
...
It's working with one cached. That PO is a macro that print the content of the object. Turns out at viewDidAppear, self.navigationItem.titleView is null.
So while I can display the searchBar, I cannot make the searchBar "fill" it's space because I do not know the space is.
I prefer not to hard code it because you know, things may change in the future.
So what should I do?
I once saw codes where rather than setting the self.navigationItem.titleView, you would simply add subview to it. The problem with this approach even on viewDidAppear, self.navigationItem.titleView is 0.
I added these codes:
CGRect theFrame= self.navigationItem.titleView.frame;
CGRect theFrame2 = self.searchBar.frame;
CGRect theFrame3 = self.navigationController.navigationItem.titleView.frame;
And, I do not know how to nslog structure value, however, theFrame and theFrame3 are all 0
You can try this inside viewWillAppear:
UIView *customTitleView = [[UIView alloc]initWithFrame:CGRectMake((320-210)/2, 0, 210, 50)];
customTitleView.backgroundColor = [UIColor clearColor];
//create your UITextField or UILabel or other view and add as subview of customTitleView
self.navigationItem.titleView = customTitleView;

Proper contentSize of UIScrollView with UIImageView inside it

I'm placing a UIImageView inside of a UIScrollView, basing my code off of the answer in this question. The problem I'm having is that there is a significant amount of white space to the bottom and right, and I can't scroll to some of the image in the top and left. I figure this is due to me incorrectly setting the contentSize of the scrollView. Here's the relevant code:
- (void)viewDidLoad
{
[super viewDidLoad];
_imageView.image = _image;
_imageView.bounds = CGRectMake(0, 0, _imageView.image.size.width,_imageView.image.size.height);
_scroller.contentSize = _imageView.image.size;
}
The view controller I'm in has three properties, a UIScrollView (_scroller), a UIImageView (_imageView), and a UIImage (_image).
You're setting the UIImageView's bounds property. You want to be setting its frame property instead. Setting the bounds will resize it around its center point (assuming you haven't changed the underlying CALayer's anchorPoint property), which is causing the frame origin to end up negative, which is why you can't see the upper-left.
_imageView.frame = CGRectMake(0, 0, _imageView.image.size.width, _imageView.image.size.height);
Alternate syntax:
_imageView.frame = (CGRect){CGPointZero, _imageView.image.size};

How do you determine the size/frame of a custom UINavigationItem.titleView?

After creating a custom view and assigning it to the navigationItem.titleView property it is displayed like this
with the custom view filling the space between the two buttons. Therefore, the custom view is not centered on the navigation bar. How do I determine the frame of the view in the .titleView property? I want to center some text in the navigation bar, say under the time stamp.
If you really want to get titleView's frame (in your top-level view's coordinate space), you can do this:
[self.navBar layoutIfNeeded];
CGRect titleViewFrameInTopLevelViewSpace = [self.navigationItem.titleView
convertRect:self.navigationItem.titleView.bounds
toView:self.view];
You need to do layoutIfNeeded if you have just assigned titleView, because by default the navigation bar won't lay out its subviews until the next pass through the run loop.
That said, the titleView will be centered automatically, if it fits. I think you are setting the frame (or bounds) of your custom view too large. I tested this two ways:
I set up the titleView directly in the XIB. I simply dragged a View from the Object library onto the center of the navigation bar:
It sized the view to 128x33 automatically. The resize handles let me adjust the size. It stays centered until it overlaps the Categorize button. Then it shifts left.
I set the titleView property in viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 33)];
customView.backgroundColor = [UIColor greenColor];
self.navItem.titleView = customView;
}
The result looks like this:
You could get the width of the leftBarButtonItem and the rightBarButtonItem after you've set them, and then use that to determine how to centre within the view you supply to titleView. That might do what you want?

Question on UIScrollView and UIImageViews

I am making an iPad app that will display six, side by side UIImageViews at once (3 on each line). I want it to be scrollable so I can display up to 15. So its basically like 5 rows of 3 UIImageViews.
I assume I will have to position the UIImageViews programatically and not using IB - is this right? If so, what do I put their positions as? So I know the positions for the first 6 UIImageViews, but what about the rest? And how do I make them appear on UIScrollView in a scrollable manner?
Is there a way to do that using IB too - or is it done all programatically?
Thank you for your help,
You need to position them in code
You can position a UIImageView like so:
UIImageView *image = [[UIImageView alloc] init];
image.frame = CGRectMake(0, 0, 50, 50);
[myScrollView addSubview: image];
This creates an image view and places it at 0, 0 in the scrollview with a width and height of 50.
You need to positon your elements like so, then when you get to the elements that will be offscreen you keep going like:
UIImageView *image1 = [[UIImageView alloc] init];
image1.frame = CGRectMake(0, 1100, 50, 50);
[myScrollView addSubview: image1];
This positions the element offscreen in the scrollview
to make the scollview scroll you need to set the contentSize of the scrollview to be bigger than the screen ie. the size of all your elements together
So if you have 6 images that are 500 pixels in height the height on your contentSize will be 6 x 500
To set the content size:
myScrollView.contentSize = CGSizeMake(768, 3000);
No its not necessary to create images by code you can do it by making a view controller and in its xib add three UIImageView as you want and then use a page control with UIScrollView and for more detail follow the tutorial -
http://www.edumobile.org/iphone/iphone-programming-tutorials/pagecontrol-example-in-iphone/

How To Draw A Line Dividing The Cell's Accessory View

I just wanted to know how I could draw a line dividing the cell's accessory view from the rest of the cell.
You could simply add a thin UIView (or UIImageView if you wanted a fancier divider) like so.
UIView *divider = [[UIView alloc] initWithFrame:CGRectMake(290, 3, 1, 38)];
[divider setBackgroundColor:[UIColor blackColor]];
[cell.contentView addSubview:divider];
[divider release];
If you're using one of UITableView's default accessory views, your content view will be squashed to make room for the accessory, so you your x-coordinate may have to change to sit at the very edge of the content view.