I've programmatically created a UISlider in viewDidLoad using the following code and when a button is pressed I want to hide the object and maybe use it again. I can't seem to get it to work. I've tried a number of approaches which all build correctly but none of which have the desired effect.
CGRect frame1 = CGRectMake(-5.0, 290.0, 100.0, 10.0);
UISlider *sliderSaveurFloral = [[UISlider alloc] initWithFrame:frame1];
[sliderSaveurFloral addTarget:self action:#selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
[sliderSaveurFloral setBackgroundColor:[UIColor clearColor]];
sliderSaveurFloral.minimumValue = 0.0;
sliderSaveurFloral.maximumValue = 50.0;
sliderSaveurFloral.continuous = YES;
sliderSaveurFloral.value = 0.0;
[self.view addSubview:sliderSaveurFloral];
CGAffineTransform trans2 = CGAffineTransformMakeRotation(M_PI * -0.5);
sliderSaveurFloral.transform = trans2;
[sliderSaveurFloral setValue:0];
UISlider inherits from UIView. I believe you can set the hidden property to make it invisible.
sliderSaveurFloral.hidden = YES; //Set it back to NO when you want it appear again
You may also have to make it not interactive when hidden,
sliderSaveurFloral.userInteractionEnabled = NO; //Set it back to YES later when you need
Related
As I am new in iphone development. I want to give glow effect like "info UIButton" to "custom UIButton".
-->
Please help me in this case, to give the above effect on button tap.
I found an easy way to do as I want.
It's by enabling check mark of "Shows Touch On Highlight" property.
See the screenshot..
It's working fine for my custom buttons.
if you do not want to use image then use below code
import 'CoreGraphics framework' to use below code
-(IBAction)myButtonTapped:(id)sender
{
UIButton *tmpbtn=(UIButton *)sender;
tmpbtn.layer.shadowColor = [UIColor cyanColor].CGColor;
tmpbtn.layer.shadowRadius = 10.0f;
tmpbtn.layer.shadowOpacity = 1.0f;
tmpbtn.layer.shadowOffset = CGSizeZero;
}
To remove glow effect just add
-(void)removeBtnGlow
{
myBtn.layer.shadowColor = [UIColor clearColor].CGColor;
myBtn.layer.shadowRadius = 10.0f;
myBtn.layer.shadowOpacity = 1.0f;
myBtn.layer.shadowOffset = CGSizeZero;
}
you cut the image of glow effect like and use this code
[yourBtn setBackgroundImage:[UIImage imageNamed:#"yourimagename"] forState:UIControlStateHighlighted];
Have you tried to create an image of a glowing version of your button icon and set it in code like this:
[yourButton setImage:glowImage forState:UIControlStateHighlighted];
or in the interface builder:
As a UIBUtton category and by using glowWithProgress: you can animate glow change
#implementation UIButton (Extra)
-(void)addGlow{
[self glowWithProgress:1.0];
}
-(void)removeGlow{
[self glowWithProgress:0.0];
}
-(void)glowWithProgress:(CGFloat)progress{
CGFloat realProgress = MAX(.0f, MIN(1.0f, progress));
self.layer.shadowColor = [UIColor whiteColor].CGColor;
self.layer.shadowRadius = 10.0f;
self.layer.shadowOpacity = 1.0f * realProgress;
self.layer.shadowOffset = CGSizeZero;
}
#end
In my app i am creating view controller with mixed of UILabel and UITextview which want to be scrollable because the text are dynamic and also exceeds vertical screen size.
I am currently having Scrollview which has subviews as below. Views are created in Xcode 4.3 Storyboard.
UILabel1(Say Heading)
UITextView1(Dynamic text which can be any size)
UILabel2(Second Heading)
UITextView2(Dynamic text which can be any size)
and so on.
The problem is
When the UITextView1 has more content then it overlaps with UILabel2 which i don't want.
I would like to have UILabel1 on top of scrollView and UITextView1 below the UILabel1. UILabel2 below UITextView1 and so on.
What i have to do to achieve this?
EDIT
In Storyboard
![enter image description here][1]
In Simulator
![enter image description here][2]
Thanks for your help guys. Much appreciated.
Code
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[scrollView setScrollEnabled:YES];
[self.view addSubview:cockTailNameLabel];
[self.view insertSubview:txtIngredients belowSubview:cockTailNameLabel];
[self.view insertSubview:scrollView belowSubview:cockTailNameLabel];
//[scrollView]
[self.cockTailNameLabel setText:self.passcockTailName];
[_txtUse setText:self.passUse];
[_txtUse setEditable:NO];
[_txtUse setUserInteractionEnabled:NO];
CGRect useFrame = _txtUse.frame;
useFrame.size.height = _txtUse.contentSize.height;
_txtUse.frame = useFrame;
[txtIngredients setText:self.passIngredients];
[txtIngredients setEditable:NO];
[txtIngredients setUserInteractionEnabled:NO];
CGRect ingredientFrame = txtIngredients.frame;
ingredientFrame.size.height = txtIngredients.contentSize.height;
txtIngredients.frame = ingredientFrame;
[txtRecipe setText:self.passReceipe];
[txtRecipe setEditable:NO];
[txtRecipe setUserInteractionEnabled:NO];
CGRect recipeFrame = txtIngredients.frame;
recipeFrame.size.height = txtRecipe.contentSize.height;
txtRecipe.frame = recipeFrame;
[scrollView insertSubview:_txtUse belowSubview:cockTailNameLabel];
[scrollView insertSubview:titleIngredients belowSubview:_txtUse];
[scrollView insertSubview:txtIngredients belowSubview:titleIngredients];
[scrollView insertSubview:btnReceipe belowSubview:txtIngredients];
[scrollView insertSubview:btnHome belowSubview:txtIngredients];
[scrollView insertSubview:txtRecipe belowSubview:btnHome];
[scrollView insertSubview:btnfacebookShare belowSubview:txtRecipe];
[scrollView insertSubview:btnTwitterShare belowSubview:txtRecipe];
/*[scrollView addSubview:_txtUse];
[scrollView addSubview:titleIngredients];
[scrollView addSubview:txtIngredients];
[scrollView addSubview:btnReceipe];
[scrollView addSubview:btnHome];
[scrollView addSubview:txtRecipe];
[scrollView addSubview:btnfacebookShare];
[scrollView addSubview:btnTwitterShare];*/
[scrollView setContentSize:CGSizeMake(320, 1000)];
NSLog(#"RecipeName :%# ",passcockTailName);
}
In Storyboard or IB you can rearrange them freely.
In code you do - (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview.
In code (in viewDidLoad):
UIScrollView *scroll =[[UIScrollView alloc] initWithFrame:CGrectMake(0,0 320, 480)];
[self.view addSubview:scroll];
// code to init your UI object
UILabel *uilabel1 = [[UIlabel alloc] initWithFrame:CGrectMake(10,10, 100, 40)]; // example
uilabel1.font = [UIFont systemFontOfSize:10];
uilabel1.text = #"UILabel1";
uilabel1.backgroundColor = [UIColor clearColor];
//
//
UILabel *uilabel2 = [[UIlabel alloc] initWithFrame:CGrectMake(10, 10 + 10 + uilabel1.frame.origin.y, 100, 40)]; // example
uilabel2.font = [UIFont systemFontOfSize:10];
uilabel2.text = #"UILabel2";
uilabel2.backgroundColor = [UIColor clearColor];
//
//
[scroll addSubview:uilabel1];
[uilabel1 releale];
[scroll addSubview:uilabel2];
[uilabel2 releale];
//
//
// in end
float offset = 10.0 * 2; // offset between uiobjects, N - number of objects
scroll.contentSize = CGSizeMake (0, 0, uilabel1.frame.size.height + uilabel2.frame.size.height + offset, 320);
Note that you may set frame (and other properties) of yours uiobjects and adds it in order to descend.
You can tell next view to start from the end of the first view like so:
startYOfNextView = firstView.frame.origin.y position + firstView.frame.size.height;
Do the same for the rest of the other view. If your view has variable text length, you may need to precaculate the height of the string based on a specific font e.g.:
CGSize maxSize = CGSizeMake(widthOfView, 9999999);
CGSize expectedSize = CGSizeMake(stringVar sizeWithFont:[UIFont fontWithName:#"Arial"] withMaxSize:maxSize lineBreakMode:UILineBreakModeWordWrap);
then tell your dynamic view to use the height of expectedSize variable like so:
myDynamicView = [[UILabel alloc] initWithFrame:CGRectMake(..., expectedSize.height)];
Your issue is labels are coming on top of the textview (Overlapped), right?
In This case, you are modifying the height of the textview dynamically. If its just for display purpose you can set the text to a UILabel with numberOfLines = 0 instead of UITextview; As its added to scrollview adding a label will be fine.
Also you need to modify the origin.y of the remaining views so that it will be properly aligned. ie, secondView.origin.y = firstView.origin.y + firstView.size.height + offset. Likewise modify the origin of the remaining views wrt the just above view. So that its origin will lie outside the previous view frame.
In my case I had to project it over the parent view, so remember there is also something,
- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview
I think adding a transparent UI button is the fastest solution. You could add it once and then show / hide whenever you need to disable all views below the current one (assuming the "current" view, say a popup, was added last to your superview).
Declare an instance variable UIButton* _parentDisablingOverlay; in your popup view, then add this in its init or layoutSubviews method:
_parentDisablingOverlay = [[UIButton alloc] initWithFrame:CGRectMake(0,0, self.superview.frame.size.width, self.superview.frame.size.height)];
[self.superview insertSubview:_parentDisablingOverlay belowSubview:self];
_parentDisablingOverlay.hidden = true;
Later on, when you want to show the popup and disable everything below:
- (void) open {
self.hidden = false;
_parentDisablingOverlay.hidden = false;
}
Similarly to close and re-enable everything:
- (void) close {
self.hidden = true;
_parentDisablingOverlay.hidden = true;
}
I want to create a custom UISwtich with three positions. Is it possible?
You should be using UISegmentedControlif you want a standard UI-Element or configure a UISlider with a range of 2:
slider.minimumValue = 0;
slider.maximumValue = 2;
slider.continuous = NO;
And then set the minimumValueImage, maximumTrackImage and thumbImage to use appropriate images.
Not using the built-in UISwitch. You'll need to roll your own.
Why not use a UISegmentedControl?
Using UISlider is a good approach. But you would additionally want to adjust the mechanics of your UISlider to be more UISwitch-like. I.e., when you change its position incompletely then it should bounce back to the home position.
Here's what I ended up doing (using a part of FelixLam's answer):
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(screenRect.size.width*0.5-width/2, screenRect.size.height*0.95-height, width, height)];
slider.minimumValue = 0;
slider.maximumValue = 2;
slider.continuous = NO;
slider.value = 1;
[slider addTarget:self action:#selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
Along with...
- (void)sliderAction:(UISlider *)slider {
float origValue = slider.value;
[UIView beginAnimations:nil context:NULL];
if (slider.value<1.9 && slider.value>0.1) slider.value=1;
else if (slider.value>1.9) slider.value=2;
else slider.value=0;
[UIView setAnimationDuration:0.2*fabs(slider.value-origValue)];
[UIView commitAnimations];
}
I've added some buttons to an UIView (via addSubview) programmatically. However, they appear as overlays (so that I always see the last button only). How do I add new buttons below existing buttons?
Regards
you can offset the button like this
int newX = previousButton.frame.origin.x + previousButton.frame.size.width ;
int newY = previousButton.frame.origin.y ;
and either set the frame for new button when you create it:
[[UIButton alloc] initWithFrame:CGRectMake(newX,newY,100,100)];
or set the frame later
newButton.frame = CGRectMake(newX,newY,100,100);
Set the UIView's frame origin to layout the UIButtons in the locations you wish:
CGRect buttonFrame = button.frame;
buttonFrame.origin = CGPointMake(100.0f, 100.0f);
button.frame = buttonFrame;
view.addSubview(button);
You can either use the insertSubview:atIndex method or insertSubview:belowSubview of your view.
UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0,100,100)];
[myView insertSubview:myButton belowSubview:previousButton];
OR
[myView insertSubview:myButton atIndex:0];
Thanks for your answers guys.
I did the (horizontal) align with this code:
if([myContainer.subviews lastObject] == nil){
NSLog(#"NIL");
[myContainer insertSubview:roundedButton atIndex:0];
}else{
[myContainer insertSubview:roundedButton belowSubview:[tagsContainer.subviews lastObject]];
}
It works technically, but still overlays the buttons. I have to find a way, how to not overlay them...
I'm programatically adding two custom UIPickerViews to another view (MainView). They work just fine but they are not visible until a touch event occurs in any part of the MainView. I've checked the class references for UIPickerView and UIView but haven't found anything that manages to "refresh" the view, unless I'm missing something obvious?
Here's my drawRect method in MainView.m. I've tried doing the same thing in viewDidLoad but with no success. Could the custom rotations/transforms etc have something to do with it?
- (void)drawRect:(CGRect)rect {
CGRect pickerFrame = CGRectMake(50, -32, 30, 180);
m_picker1 = [[UIPickerView alloc] initWithFrame:pickerFrame];
m_picker1.delegate = self;
m_picker1.tag = k_ptag1;
m_picker1.showsSelectionIndicator =YES;
m_picker1.backgroundColor = [UIColor clearColor];
CGAffineTransform rotate = CGAffineTransformMakeRotation(3.14/2);
rotate = CGAffineTransformScale(rotate, 0.075, 0.85);
[m_picker1 setTransform:rotate];
[self addSubview:m_picker1];
pickerFrame = CGRectMake(50, 67, 30, 180);
m_picker2 = [[UIPickerView alloc] initWithFrame:pickerFrame];
m_picker2.delegate = self;
m_picker2.tag = k_ptag2;
m_picker2.showsSelectionIndicator =YES;
m_picker2.backgroundColor = [UIColor clearColor];
rotate = CGAffineTransformMakeRotation(3.14/2);
rotate = CGAffineTransformScale(rotate, 0.075, 0.85);
[m_picker2 setTransform:rotate];
[self addSubview:m_picker2];
}
You add subviews in a view's controller, not the view itself. I would suggest familiarising yourself with the MVC design pattern.
drawRect is only supposed to be used for the actual drawing of the view itself, not subviews.