How do you make a slider that adjusts the volume level of the device with storyboarding? I haven't found any information on this relevant to XCode 4. Is MPVolumeView still the way to go? If so, how do I implement it?
Thanks.
You can still use an MPVolumeView in Xcode 4.x and iOS5. Simply import the MediaPlayer framework (link to it in your project settings, too) and use something like this:
MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:CGRectMake(0,0,120,15)];
[someView addSubview:volumeView];
This will give you the slider that will change system volume when dragged.
If you want to use a slider, you can add one to your view and link it to an action in the corresponding view controller. The action looks like this:
- (IBAction)volumeSliderChanged:(id)sender
{
UISlider *slider = (UISlider *)sender;
float newVolume = slider.value;
// Set new volume
}
Related
I am struggling to change background properties of UIButton.
They can be easily modified using "Background Configuration" settings in Interface Builder:
but I don't understand how to set them programmatically.
For example, I try to do it like this:
UIBackgroundConfiguration * config = [UIBackgroundConfiguration listPlainCellConfiguration];
[config setStrokeColor: UIColor.labelColor];
but where should I assign this 'config' value? There is nothing like 'button.backgroundConfiguration' property or anything similar for UIButton object.
Note:
'configuration' property of UIButton object is not available in code (Objective C, Mac Catalyst, iOS 15) despite documentation statement, screenshot attached. Will be great to know the reason why this is so.
The background configuration is part of the UIButtonConfiguration (the button's configuration).
https://developer.apple.com/documentation/uikit/uibuttonconfiguration/3750780-background?language=objc
Here's an example of configuring a button created in the storyboard:
UIButtonConfiguration * con = self.button.configuration;
[con setTitle: #"Hello"];
UIBackgroundConfiguration * back = con.background;
[back setStrokeColor: UIColor.labelColor];
[back setStrokeWidth: 3];
[back setBackgroundColor: [UIColor redColor]];
self.button.configuration = con;
Result:
It happens that 'button.configuration.background' property is not available in the Xcode 13 beta 5 for Mac Catalyst.
One have to wait for newer Xcode release to get this feature working from code.
I was using InAppSettings to manage my settings but it stopped working in iOS 7 and I couldn't figure out why. So I set out to build my own simple settings tableView. In cellForRowAtIndexPath: I make a slider and add it to my cell like this:
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(really ugly frame code)];
slider.minimumValue = 0;
slider.maximumValue = 7;
slider.value = 4;
slider.userInteractionEnabled = YES;
[slider addTarget:self action:#selector(slide:) forControlEvents:UIControlEventValueChanged];
[cell addSubview:slider];
The slider shows up perfectly, but I can't interact with it! No matter what I do it won't move. And the target action never gets fired either. The exact same bug I was having with InAppSettings to begin with.
It feels like I am missing something simple, anybody see this before?
Weird. I guess I was initializing the UISlider incorrectly. This code works.
I need to add a left bar button item in my app that looks like system back button, but is not the system back button, since it will appear on view controller that is the only vc of my navController's stack and execute my own code. Simply writing "Back" isn't really good for me, as I need to display the "<" arrow as well. Is there a way to programmatically create an arrow like that, without making image with that arrow?
You can use any Unicode character (anywhere in your code) for this.
You can find what you want here:
Xcode > Edit > Special Characters...
Search for arrow or back or browse the categories.
Then copy paste the character where required (in the XIB object's title or in the setTitle or setText methods like any other character)
something like:
Try this:
// After you create and place your backButton:
UILabel* arrowLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 20, 20)];
arrowLabel.text = #"<";
arrowLabel.textColor = [backButton titleColorForState:UIControlStateNormal];
arrowLabel.transform = CGAffineTransformMakeScale(1,2);
[backButton addSubview:arrowLabel];
If addSubview gives you trouble, try
[backButton insertSubview:arrowLabel atIndex:0];
Consider using a dummy UIViewController as a root view controller for your UINavigationController’s stack:
[[UINavigationController alloc] initWithRootViewController:[UIViewController new]];
[navController pushViewController:viewController animated:NO];
Then you can use my BackButtonHandler extension to handle back button action (as described in this thread) :
-(BOOL) navigationShouldPopOnBackButton {
[self dismissModalViewControllerAnimated:YES];
return NO;
}
Look at the UIBarButtonSystemItem enum under contants in the UIBarButtonItem documentation:
https://developer.apple.com/library/IOs/documentation/UIKit/Reference/UIBarButtonItem_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40007519-CH3-SW2
These are all of the button styles provided by Apple:
typedef enum {
UIBarButtonSystemItemDone,
UIBarButtonSystemItemCancel,
UIBarButtonSystemItemEdit,
UIBarButtonSystemItemSave,
UIBarButtonSystemItemAdd,
UIBarButtonSystemItemFlexibleSpace,
UIBarButtonSystemItemFixedSpace,
UIBarButtonSystemItemCompose,
UIBarButtonSystemItemReply,
UIBarButtonSystemItemAction,
UIBarButtonSystemItemOrganize,
UIBarButtonSystemItemBookmarks,
UIBarButtonSystemItemSearch,
UIBarButtonSystemItemRefresh,
UIBarButtonSystemItemStop,
UIBarButtonSystemItemCamera,
UIBarButtonSystemItemTrash,
UIBarButtonSystemItemPlay,
UIBarButtonSystemItemPause,
UIBarButtonSystemItemRewind,
UIBarButtonSystemItemFastForward,
UIBarButtonSystemItemUndo, // iOS 3.0 and later
UIBarButtonSystemItemRedo, // iOS 3.0 and later
UIBarButtonSystemItemPageCurl, // iOS 4.0 and later
} UIBarButtonSystemItem;
Perhaps rewind or undo would be close enough for you.
Adding some controls to UIAlertView was deprecated in iOS7 using addSubview method. As I know Apple promised to add contentView property.
iOS 7 is released now and I see that this property is not added. That is why I search for some custom solution with ability to add progress bar to this alertView. Something for example similar to TSAlertView, but more ready for using in iOS 7.
Here is a project on Github to add any UIView to an UIAlertView-looking dialog on iOS7.
(Copied from this StackOverflow thread.)
It took me only 1 day to create my own alert view that looks exactly like Apple's
Take a screenshot of Apple's alert for reference (font sizes, spacings, width)
Create a xib with title, message, custom view and tables for buttons (Apple uses tables instead of UIButton now, default table cell is good enough). Note you need 3 button tables: two for left and right buttons (whenever the number of buttons is 2), another one for the other cases (one button or more than 2 buttons).
Implement all the methods from UIAlertView on your custom alert.
Show/Dismiss - you can create a specific modal window for your alerts but I just put my alerts on top of my root view controller. Register your visible alerts to a static array. If showing the first alert/dismissing the last, change tint mode of your window/view controller to dimmed/to automatic and add/remove a dimming view (black with alpha = 0.2).
Blurred background - use Apple's sample code (I used opaque white)
3D dynamic effects - use Apple's sample code (5 lines of code). If you want a nice effect, take a slightly bigger snapshot in step 5 and add inverse animators for alert background and foreground.
EDIT:
Both blurred background and the paralax effect sample code can be found in "iOS_RunningWithASnap" WWDC 2013 sample code
Paralax effect:
UIInterpolatingMotionEffect* xAxis = [[[UIInterpolatingMotionEffect alloc] initWithKeyPath:#"center.x"
type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis] autorelease];
xAxis.minimumRelativeValue = [NSNumber numberWithFloat:-10.0];
xAxis.maximumRelativeValue = [NSNumber numberWithFloat:10.0];
UIInterpolatingMotionEffect* yAxis = [[[UIInterpolatingMotionEffect alloc] initWithKeyPath:#"center.y"
type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis] autorelease];
yAxis.minimumRelativeValue = [NSNumber numberWithFloat:-10.0];
yAxis.maximumRelativeValue = [NSNumber numberWithFloat:10.0];
UIMotionEffectGroup *group = [[[UIMotionEffectGroup alloc] init] autorelease];
group.motionEffects = #[xAxis, yAxis];
[self addMotionEffect:group];
The blurred background is the only complicated thing. If you can use an opaque color instead, use it. Otherwise it's a lot of experimenting. Also note that blurred background is not a good solution when the background is dark.
For the show/dismiss animationg, I am using the new spring animation method:
void (^animations)() = ^{
self.alpha = 1.0f;
self.transform = CGAffineTransformIdentity;
};
self.alpha = 0.0f;
self.transform = CGAffineTransformMakeScale(0.5f, 0.5f);
[UIView animateWithDuration:0.3
delay:0.0
usingSpringWithDamping:0.7f
initialSpringVelocity:0.0f
options:UIViewAnimationOptionCurveLinear
animations:animations
completion:^(BOOL completed) {
//calling UIAlertViewDelegate method
}];
I wrote a full implementation of UIAlertView that mimics the complete UIAlertView API, but adds the contentView property we've all wanted for so long: SDCAlertView.
(source: github.io)
For those who love simple and effective methods with out having to write lines of code. Here is a cool solution without using any other private frame works for adding subviews to ios 7 alert views,i.e.
[alertView setValue:imageView forKey:#"accessoryView"];
Sample code for better understanding,
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(180, 10, 85, 50)];
UIImage *wonImage = [UIImage imageNamed:#"image.png"];
[imageView setImage:wonImage];
//check if os version is 7 or above
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
[alertView setValue:imageView forKey:#"accessoryView"];
}else{
[alertView addSubview:imageView];
}
Hope it helps some one,thanks :)
For IOS7
UIAlertView *alertView1 = [[UIAlertView alloc] initWithTitle:#"Enter Form Name"
message:#""
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok", nil];
alertView1.alertViewStyle = UIAlertViewStyleSecureTextInput;
UITextField *myTextField = [alertView1 textFieldAtIndex:0];
[alertView1 setTag:555];
myTextField.keyboardType=UIKeyboardTypeAlphabet;
[alertView1 show];
There wont be UIAlertView with custom views in iOS7, nor contentView which Apple changed its mind about, so addSubview is impossible now in UIAlertView.
A good alternative will be SVProgressHUD, according to many threads in Apple's forum.
Edit:
There is officially no addSubview nor subclassing for UIAlertView in iOS7.
The UIAlertView class is intended to be used as-is and does not
support subclassing. The view hierarchy for this class is private and
must not be modified.
Other good alternatives:
ios-custom-alertview by wimagguc
MZFormSheetController.
You can find simple solution without extra classes here
It is based on setting accessoryView for ordinary UIAlertView.
PKAlertController (https://github.com/goodpatch/PKAlertController) is great library. I tested a lot of similar libraries and just this satisfied all my requirements.
Why it is cool:
Supports custom view
Supports iOS7
It is view controller
It behaves and looks like native alert view, including motion effects
Customizable
Similar interface like in UIAlertController
I'm creating a game for iPad using OpenGL. I have created a view (with a simple background) grammatically using the following code:
- (void) addNewView {
UIWindow* window = [UIApplication sharedApplication].keyWindow;
UIView *polygonView = [[UIView alloc] initWithFrame: CGRectMake ( 60, 0, 900, 900)];
polygonView.backgroundColor = [UIColor blueColor];
//here play a movie:-)
[window addSubview:polygonView];
[polygonView release];
}
and i'm display it using the following code within a case switch in a window:
[self addNewView];
This all works well.
However, I need help to implement a video feature in this view, when it is displayed.
Can anyone provide me with assistance in the form of code or a link to a relevant tutorial that can aid me with this?
Take a look at:
MPMoviePlayerController Class Reference
and
MPMoviePlayerViewController Class Reference
That should get you started.