How iOS Development customizes content in Uiactionsheet (Uialertview)

Source: Internet
Author: User
Tags uikit

Original address: http://blog.csdn.net/sjzsp/article/details/6688950

Because the UI has a special purpose, uiactionsheet and Uialertview do not allow you to customize the interface by addsubview such functions. The solution is to inherit it, implement a custom class, overload the Layoutsubviews function//. h
#import < uikit/uikit.h >

@interface Uiimageactionsheet:uiactionsheet {
UIImage * titleimage;
}
-(ID) initwithimage: (UIImage *) image
Title: (NSString *) title
Delegate:(ID < uiactionsheetdelegate >) delegate
Cancelbuttontitle: (NSString *) cancelbuttontitle
Destructivebuttontitle: (NSString *) destructivebuttontitle
Otherbuttontitles: (NSString *) otherbuttontitles;

@end

. m file
#import "UIImageActionSheet.h"


@implementation Uiimageactionsheet
-(ID) initwithimage: (UIImage *) image
Title: (NSString *) title
Delegate:(ID < uiactionsheetdelegate >) delegate
Cancelbuttontitle: (NSString *) cancelbuttontitle
Destructivebuttontitle: (NSString *) destructivebuttontitle
Otherbuttontitles: (NSString *) otherbuttontitles{

self = [Super Initwithtitle:title delegate:delegate
Cancelbuttontitle:cancelbuttontitle
Destructivebuttontitle:destructivebuttontitle
Otherbuttontitles:otherbuttontitles,nil];

if (self) {
Titleimage = image;
[Titleimage retain];
Uiimageview * ImageView = [[Uiimageview alloc] initwithimage:titleimage];
Imageview.frame = Cgrectzero;
//
For (UIView * Subview in Self.subviews) {
if (! [Subview Iskindofclass:[uilabel class]] {
[Self Insertsubview:imageview abovesubview:subview];
break;
}
}

[ImageView release];
}
return self;
}


-(CGFloat) maxlabelycoordinate {
Determine maximum y-coordinate of labels
CGFloat Maxy = 0;
for (UIView * view in Self.subviews) {
if ([View Iskindofclass:[uilabel class]]) {
CGRect viewframe = [view frame];
CGFloat LowerY = viewframe.origin.y + viewFrame.size.height;
if (LowerY > Maxy)
Maxy = LowerY;
}
}
return Maxy;
}

-(void) layoutsubviews{
[Super Layoutsubviews];
CGRect frame = [self frame];
CGFloat labelmaxy = [self maxlabelycoordinate];

for (UIView * view in Self.subviews) {
if (! [View Iskindofclass:[uilabel class]] {
if ([View Iskindofclass:[uiimageview class]]) {
CGRect viewframe = CGRectMake ((320-titleimage.size.width)/2, Labelmaxy + 10,
TitleImage.size.width, TitleImage.size.height);
[View Setframe:viewframe];
}
else if (! [View Iskindofclass:[uiimageview class]] {
CGRect viewframe = [view frame];
VIEWFRAME.ORIGIN.Y + = TitleImage.size.height + 10;
[View Setframe:viewframe];
}
}
}

FRAME.ORIGIN.Y-= titleImage.size.height + 2.0;
Frame.size.height + = titleImage.size.height + 2.0;
[Self setframe:frame];

}
/*
Only override drawrect:if you perform custom drawing.
An empty implementation adversely affects performance during.
-(void) DrawRect: (cgrect) Rect {
Drawing code.
}
*/

-(void) Dealloc {
[Super Dealloc];
if (titleimage) {
[Titleimage release];
}
}


@end

Use:

UIImage *tips3img = [uiimage imagenamed:@ "Tips-3.png"]; Uiimageactionsheet * Tipsactionsheet = [[Uiimageactionsheet alloc]
Initwithimage:tips3img
The pattern added by title: @ "can be adjusted in the following ways"
Delegate:self cancelbuttontitle: @ "Know"
Destructivebuttontitle:nil
Otherbuttontitles:nil];
Tipsactionsheet.tag = kactionsheettagtips;
[Tipsactionsheet ShowInView:self.view];
[Tipsactionsheet release];



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.