Center iOS view hierarchy in center of superview - ios

iOS view hierarchies are doing my head in - I want to achieve something that should be basic.
In a UIWebView I want to center a UIActivityIndicatorView with a UILabel below it in the middle of the UIWebView. The activity indicator and the label can be considered as children of another UIView - and it's that parent UIView that needs to be centered in the UIWebView
How does one go about this? How do you get the frame of the parent UIView to be the bounding rect of it's children?

Both views should be sub views of a super view...
UIView *activityView = //the view with the label and activity indicator...
UIView *webView = //the web view...
activityView.center = webView.center;
Done :)

Related

UIScrollView is not responded when there are UIView on the same level

I have a container view which contains an UIScrollView and an UIView, that are at the same level with the same frame and bounds, meaning they are subviews of the container view.
But the UIView is on the top of the UIScrollView. So the problem is that the buttons or gestures on the UIView are responded to my touches, while the UIScrollView is not, either scrolling or zooming.
So how can I make the UIScrollView respond to scrolling and zooming and UIView respond to touches at the same time when these two views are on the same level of the same super view?
UPDATE:
What I'm trying to do is I want to have a player control panel view which contains play/pause button, progress button etc., and behind this panel view there is a player render view which is in charge of rendering the video frames.
And I want to make the render view be able to be zoomed and scrolled, while the control panel view stays still.
The sample code is like this:
#implementation MyVC: UIViewController <UIScrollViewDelegate>
- (void)viewDidLoad
{
UIView *view = [UIView new];
UIScrollView *scrollView = [UIScrollView new];
[self.view addSubview:scrollView];
[self.view addSubview:view];
view.frame = scrollView.frame = self.view.frame;
//zooming and scrolling settings code
//now the problem is that scrollview does not respond to my zooming and scrolling
//while the view does.
}
#end
Maybe you can try this:
[view addGestureRecognizer:scrollView.panGestureRecognizer];
Or add any other UIGestureRecognizer in scrollView.gestureRecognizers to view. But it possibly will cause some issues when zooming. I did not check it. Maybe adding your buttons play/pause to the container view on the level same as scrollView is a better way.

Show view/point/smt under clipped area

Is it possible to show something under clipped area?
For example I have:
UIView *viewA = ...
viewA.layer.cornerRadius = radius;
viewA.layer.masksToBounds = YES;
So, here I have view with rounded corners. Then I want to add another viewB as subview of viewA and present it under clipped area of viewA. Help me, please, how can I do that?
You mean that you want viewB to be clipped too or that it is shown in the clipped area?
If it is the former:
viewA.clipsToBounds = YES;
UIView *viewB = ...
viewB.frame = ... // something relative to viewA.bounds
[viewA addSubview:viewB];
EDIT:
Then for what you want to do you have to set viewA.clipsToBounds = YES, create a container view for both viewA and viewB and put them both as subviews of this container. You can't clip a superview and have an unclipped subview.

how to scroll to the center position of the object

Hi Im making a simple game with the basic UIKit stuff where there is an object that pops up on the screen.
I'm trying to make it so that every time the object pops up the screen will scroll to the object and center it on screen. Basically I have a UIView that has the objects that popup within it and that UIView is a subview of a UIScrollView. All of this is a subview of self.view of my ViewController. I'm using the UIScrollview to scroll through the contents of its subview with the objects in it. I'm not sure if a UIScrollView is the ideal option for what I want to do but I was looking for a way to scroll the position of the object and center it in the middle of the device. Ideally with animation.
If anyone knows of a method or maybe a formula that could help me out that would be awesome. Thanks in advance :D
Update:
I don't really have much code in regards to this except for adding the views within each other :
self.world = [[UIView alloc]initWithFrame:CGRectMake(0, 0, currentDeviceWidth*4, currentDeviceHeight*2.2)];
self.world.backgroundColor = [UIColor blueColor];
_scrollView = [[UIScrollView alloc] initWithFrame:[[self view] bounds]];
[_scrollView setContentSize:self.world.frame.size];
[_scrollView addSubview:self.world];
[[self view] addSubview:_scrollView];
but below is a simple image of what I want to accomplish. The arrows are just a representation of the possible scrolling direction.
Apple object is off the side of the screen Note: apple object is a subview of self.world and self.world is a subview of scrollview
The finish look is where now the scrollview has scroll self.world to where the apple object is in the center of the screen
Just looking to basically accomplish how a user could manually scroll the object to the center but instead do it automatically without the use of a user's touch.
I think you may need contentOffset which is a property of UIScrollView. This property controls the position of the contentView.
Now let's say, your apple is in the center of the self.world, then animated put it in the middle is like this:
[UIView animateWithDuration: 0.5//you can put any float as you want
animations:^{self.world.contentOffset = CGPointMake((_scrollview.frame.size.width - self.world.frame.size.width) / 2, (_scrollview.frame.size.height - self.world.frame.size.height) / 2);
}];
If you put your apple in any other position, you should calculate the position yourself, and set the contentOffset to the right one. I think any position other than center is much complicated, you calculation should involve the position of the apple in the self.world view.
Did you try
[_scrollView scrollRectToVisible:newView.frame animated:(BOOL)animated]
What might be tricky about what you are trying to do is positioning views such that they are always within the coordinate system of the _scroll view, e.g. x >= middle of the view & y >= middle of the view.

iOS how to add a view to screen center and stick it to screen center on a UITableViewController?

I'm working on a UITableViewController. I have to display a view with some info, that view will be added to the TableViewController programatically. This is my code of adding it:
MyNotificationView *notificationView = // some initializations;
[self.view addSubview:notificationView]; // self is the UITableViewController
notificationView.center = self.view.center;
However on UITableViewController, this code actually sets the center of my notificationView to the center of the scrollable area of the TableViewController, and my notificationView scrolls as the TableView scrolls.
What I want is to add my notificationView to the screen center and stick it there, so how can this be done?
You can make a UIViewController with an UITableView inside and the notificationView you want to keep in center, just add it to view [self.view addSubview:notificationView];

TopLayoutGuide offset within UIView drawing

I have a custom view class derived from MKMapView which is overlapped at the top from an UINavigationBar to get this translucent style. I found out i must implement inside the UIViewController the TopLayoutGuide to move the top compass subview from the MKMapView in its right position to be visible. This works perfect, but i also have a custom subview inside my view and i cant figure out how i get the new top offset from the UIViewController from inside the View to position my subview correctly. How did Apple this magic for their subviews inside MKMapView?
I found a way to get the top layout guide position within an UIView to reposition subviews correctly
- (void)layoutSubviews
{
UIViewController *myController = (UIViewController*)[self nextResponder];
CGFloat top = myController.topLayoutGuide.length;
// reposition subviews
// ...
}

Resources