UIView center X UIImageView - ios

In one UIVIew i have 5 icon (UIImageView). I show/hide icons. Its ok. But i want if i hide 2 icon the rest what not hidden (or not removed) show center of the View.
if show all:
http://sherad.info/elotte.jpg
I remove from view 2 icon (i try the set hiden but the effect same)
[share removeFromSuperview];
[sms removeFromSuperview];
After this:
http://sherad.info/utana.jpg
I want the 3 icon show in center of view. So somehow re arrange the icons inside the UIView. How i can solved this?
thx.

I'll assume the black area behind the icons is a UIView and the icons have been added to it in a static position? What if, instead, you created a second UIView that was transparent. Determine what icons you want to display and add them into the second view. It would start off with the second view being the exact same size as the first view, but once you want to display less, remove all the icons from the second view
for (yourIcon *icon in [secondView subviews]) {
[icon removeFromSuperview];
}
reset the frame of the second view to be the length you want and re-add the icons, then place it in the center of the first view
secondView.center = firstView.center;
and either do
[secondView setNeedsDisplay]
or re-add it as a subview of firstView if you previously removed it (which I don't believe is necessary).

The subviews should position themselves relative to each other based on how many other icons there are. Every time there is an event that add or removes an icon you should call some layout code that will make the icons postion themselves.
Removing a view should have the superview count the added subviews, create new x coordinates based on the screen width divided by the count and then set the frames of all of the subviews.
There is probably a way to do this with autolayout but I'm not familiar with those methods.

Related

UIPickerView ignoring contraints

I have a view within my app that has a UIPickerView. The delegation and datasource is set up and the picker loads data correctly, however, it ignores storyboard constraints entirely and shifts to the top of the view regardless of what I've tried.
The screenshots below show the alignment in storyboard (the picker is aligned center vertical/horizontal to the blue view (which has a constraint of 30 on all sides) and the alignment when the app is run on a 6S.
Any idea what I'm doing wrong?
UIPickerView has a fussy implementation of setFrame: -it only allows itself to be rendered in a handful of sizes. The easiest way to control/overcome this is to put it in a container view and apply your constraints to that view. You could override layoutSubviews in that containerView in order to center the picker, perhaps even applying a transform to scale it. Don't try to set the size of the pickerview in this code, just query (get) it and then calculate around that value.
Remove [self.view addSubview:self.mouthpiecePicker]; This is re-adding the picker view, but it was already added with Storyboard. When you re-add it, it removed all constraints.

How to make a popup window with an image SWIFT

I was wondering how to make a popup window similar to this example:
The origin window is full of buttons that when is selected will then pull up the image I desire to use.
I would simply create a reusable UIView component and everything you need as a subview, such as a UIImageView for your image, a UILabel or a UIButton in the top right. Here is the process to show it:
Create a UIView that takes up the full screen, make it black, and maybe 0.5 alpha.
Create another UIView which is your primary pop-up view, make it slightly smaller than the previous view, but make sure both of these views are subviews of the parent subview.
Add the desired elements on to the pop-up view as subviews, I would even suggest creating a UIView subclass if you plan to use this a lot.
To present the pop-up, make sure both views are set to hidden = true when created and so that when a button is selected, you can set them to hidden = false
If you would like them to be animated, simply start them off with alpha = 0.0 and use something like UIView's animateWithDuration and set the pop-up view to alpha = 1.0
There is a lot of little details you can change to cater to your needs, but this is the basic structure on how to accomplish your goal.
Check out UIView animation methods here.

Add a subview from within and below another subview

In an iOS app, I am adding a subview to my main view with:
[self.view addSubview:firstUIImageSubview];
However in the subview class firstUIImageSubview, I am creating and adding another subView (secondUIImageSubview) that I would like to put below the first subview with:
[self insertSubview:secondUIImageSubview belowSubview:self];
My problem is that the second subview is displayed above the first subview when I want to have it below. How is it possible to achieve that ? Thanks.
This should do the trick.
[self.superview insertSubview:secondUIImageSubview atIndex:0];
When you use insertSubview:belowSubview: it places the subview in regards to other subviews that particular object manages.
[self insertSubview:secondUIImageSubview belowSubview:self];
Doesn't make much sense. Although self is a UIView (or a subclass) it still should never manage itself as a subview. Therefore
[self insertSubview:secondUIImageSubview belowSubview:firstUIImageSubview];
is probably what you want. But remember this will only place the secondUIImageSubview below firstUIImageSubview in terms of its Z-Index (it's depth on the screen). If you want it to be physically placed below firstUIImageSubview (IE it's XY coordinate) then you need to set it's position using subview's frame or setting its origin instead (by manipulating it's center or anchor points for instance).

moving image behind label

I have an image on my view, which I add programmatically & and not through storyboards. The image gets animated to move up the screen. At one point it crosses paths with several buttons and labels, which contain important text that gets blocked by the image passing over them. I want the image to move under the labels and buttons so that the text remains visible. Based on this answer IPhone Interface Builder: Z-Index, Z-order of a button, image, ui element, etc?, I tried to use editor => arrange => sendToBack/sendToFront, however, I don't think this is having any effect because the image doesn't appear in the tree of elements in the storyboard. As noted, it gets added programmatically at a certain time. Is there a way to do this?
I believe you can use [view insertSubview:imageView atIndex:0] which will place it behind all other subviews. Depending on what other subviews you have, you may need to increase the index as it controls what is placed over what in the view (lower index will go behind higher index)
As in answer to this question: Difference between addSubview and insertSubview in UIView class
If you're using addSubview:, your view will be placed on top. To control it's depth use either insertSubview:atIndex: (at exact place from bottom) or insertSubview:aboveSubview: or insertSubview:belowSubview: (which places new view above/below another known object in view hierarchy).

How can I add a scroll view to my iOS application?

I am creating an application for the iPhone which involves having buttons which animate a image view, I created another view in which the buttons are held, however I would like to be able to horizontally or vertically, scroll this view of buttons. I have looked into adding scroll views and I have even followed a tutorial, But I just can't figure out how to add it to my app. Any Ideas anyone? In the tutorial I followed the guy adds the scroll view to the MainWindow.xib, now my app is created and designed in the viewcontroller.xib. So after following the tutorial when I hit build and go it loads that nib and all you see is a white background and a scroll view. (also in that tutorial he scrolled an image where as I would like to scroll a long view of buttons) I realize the newbie-ness of my question could be crazy, but I'm new to programming all together. Here's another additional question, if someone helped me get this scroll view working where would you design the contents of that scrolled view if it's length is longer than the iphone screen? Because It is my understanding that the space to design with in interface builder is the size of the iphone screen and designing interfaces longer than the iphone screen isn't do-able. (of course I know it is do-able I just don't know how to do it).
You can set the content view (scrolling area) dimensions programmatically:
[scrollView setContentSize:CGSizeMake(contentViewWidth, contentViewHeight)];
And then add you view components to the scroll view using the coordinate system of the content view. So say your scroll view was 100x100 pixels and you wanted the scroll view to be twice as wide:
[scrollView setContentSize:CGSizeMake(200.0f, 100.0f)];
You could then add a button to appear on each half of the scroll view:
// First half of content view
button1.frame.origin.x = 10.0f;
button1.frame.origin.y = 50.0f;
[scrollView addSubView:button1];
// First half of content view
button2.frame.origin.x = 110.0f; // NOTE that 110 is outside of
button2.frame.origin.y = 50.0f; // the scroll view frame
[scrollView addSubView:button2];
You could probably design each page of the scroll view as a separate sub view in the NIB and then shift the sub views origin to the correct page location either in Interface Builder or programmatically - but I have never tried this. If this works you'd then be able to layout your buttons using IB.
I would be interested to know if this works.
most views have a .hidden property.
set it to YES to hide -> myscrollview.hidden = YES;
set to NO to show -> myscrollview.hidden = NO;
you can put this hide/show inside an animation and fade out the scrollview.
look-up "simple quartz animation" - it only a few lines of code.

Resources